前言:在Android開發(fā)的過程中,必須會(huì)接觸到數(shù)據(jù)交互(訪問數(shù)據(jù),寫入數(shù)據(jù)等你等),既然接觸到數(shù)據(jù)的交互,那么自然而然就是使用通訊間的協(xié)議來進(jìn)行請求,最常見的協(xié)議就是Http協(xié)議,Http協(xié)議包括兩個(gè)具體的請求方式-Get以及Post。
- Http請求方式Get與Post的簡介
先來了解Http協(xié)議:Http(HyperText Transfer Protocol超文本傳輸協(xié)議)是一個(gè)設(shè)計(jì)來使客戶端和服務(wù)器順利進(jìn)行通訊的協(xié)議。
HTTP在客戶端和服務(wù)器之間以request-response protocol(請求-回復(fù)協(xié)議)工作。
簡單來說呢,Get與Post就是基于http協(xié)議的網(wǎng)絡(luò)數(shù)據(jù)交互方式。
- Get與Post的主要區(qū)別
在Android開發(fā)的過程中,該如何選擇Http的Get還是Post來進(jìn)行通訊呢?那就詳細(xì)探索他們之間的差異。
1.get通常是從服務(wù)器上獲取數(shù)據(jù),post通常是向服務(wù)器傳送數(shù)據(jù)。
2.get是把參數(shù)數(shù)據(jù)隊(duì)列加到表單的 ACTION屬性所指的URL中,值和表單內(nèi)各個(gè)字段一一對應(yīng),在URL中可以看到,實(shí)際上就是URL拼接方式。post是通過HTTPpost機(jī)制,將表單內(nèi)各個(gè)字段與其內(nèi)容放置在HTML HEADER內(nèi)一起傳送到ACTION屬性所指的URL地址。
3.對于get方式,服務(wù)器端用 Request.QueryString獲取變量的值,對于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é)議
不多說,上代碼展示(演示用戶登錄訪問服務(wù)器)
public class LoginServer {
/**
*get的方式請求
*@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){
//請求成功 獲得返回的流
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//請求失敗
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** * post的方式請求
*@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è)請求頭
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){
//請求成功
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//請求失敗
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}