前言:在Android開發(fā)的過(guò)程中,必須會(huì)接觸到數(shù)據(jù)交互(訪問(wèn)數(shù)據(jù),寫入數(shù)據(jù)等你等),既然接觸到數(shù)據(jù)的交互幌甘,那么自然而然就是使用通訊間的協(xié)議來(lái)進(jìn)行請(qǐng)求,最常見(jiàn)的協(xié)議就是Http協(xié)議,Http協(xié)議包括兩個(gè)具體的請(qǐng)求方式-Get以及Post吃谣。
- Http請(qǐng)求方式Get與Post的簡(jiǎn)介
先來(lái)了解Http協(xié)議:Http(HyperText Transfer Protocol超文本傳輸協(xié)議)是一個(gè)設(shè)計(jì)來(lái)使客戶端和服務(wù)器順利進(jìn)行通訊的協(xié)議。
HTTP在客戶端和服務(wù)器之間以request-response protocol(請(qǐng)求-回復(fù)協(xié)議)工作做裙。
簡(jiǎn)單來(lái)說(shuō)呢岗憋,Get與Post就是基于http協(xié)議的網(wǎng)絡(luò)數(shù)據(jù)交互方式。
- Get與Post的主要區(qū)別
在Android開發(fā)的過(guò)程中锚贱,該如何選擇Http的Get還是Post來(lái)進(jìn)行通訊呢澜驮?那就詳細(xì)探索他們之間的差異。
1.get通常是從服務(wù)器上獲取數(shù)據(jù)惋鸥,post通常是向服務(wù)器傳送數(shù)據(jù)杂穷。
2.get是把參數(shù)數(shù)據(jù)隊(duì)列加到表單的 ACTION屬性所指的URL中悍缠,值和表單內(nèi)各個(gè)字段一一對(duì)應(yīng),在URL中可以看到耐量,實(shí)際上就是URL拼接方式飞蚓。post是通過(guò)HTTPpost機(jī)制,將表單內(nèi)各個(gè)字段與其內(nèi)容放置在HTML HEADER內(nèi)一起傳送到ACTION屬性所指的URL地址廊蜒。
3.對(duì)于get方式趴拧,服務(wù)器端用 Request.QueryString獲取變量的值,對(duì)于post方式山叮,服務(wù)器端用Request.Form獲取提交的數(shù)據(jù)著榴。
4.get 傳送的數(shù)據(jù)量較小,不能大于1KB[IE,Oher:4]屁倔。post傳送的數(shù)據(jù)量較大脑又,一般被默認(rèn)為不受限制。但理論上锐借,IIS4中最大量為80KB问麸,IIS5中為100KB。
5.get安全性非常低钞翔,post安全性較高严卖。
- Android如何使用Get與Post協(xié)議
不多說(shuō),上代碼展示(演示用戶登錄訪問(wèn)服務(wù)器)
public class LoginServer {
/**
*get的方式請(qǐng)求
*@param username 用戶名
*@param password 密碼
*@return 返回null 登錄異常
*/
public static String loginByGet(String username,String password){
//get的方式提交就是url拼接的方式
String path = "http://172.16.168.111:1010/login.php?username="+username+"&password="+password;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
//獲得結(jié)果碼
int responseCode = connection.getResponseCode();
if(responseCode ==200){
//請(qǐng)求成功 獲得返回的流
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//請(qǐng)求失敗
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** * post的方式請(qǐng)求
*@param username 用戶名
*@param password 密碼
*@return 返回null 登錄異常
*/
public static String loginByPost(String username,String password){
String path = "http://172.16.168.111:1010/login.php";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
//數(shù)據(jù)準(zhǔn)備
String data = "username="+username+"&password="+password;
//至少要設(shè)置的兩個(gè)請(qǐng)求頭
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", data.length()+"");
//post的方式提交實(shí)際上是留的方式提交給服務(wù)器
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
//獲得結(jié)果碼
int responseCode = connection.getResponseCode();
if(responseCode ==200){
//請(qǐng)求成功
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//請(qǐng)求失敗
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}