標籤:網路 post get httpurlconnection
之前在使用AsyncHttpClient的時候,遇到在Android6.0後找不到HttpClient的問題,後來官方更新了1.4.9版本替換了HttpClient為第三方的cz.msebera.android.httpclient。瞭解到Google在Android6.0後移除了HttpClient,推薦使用HttpUrlConnection實現http請求,並且許多其他第三方網路請求架構都是改為以HttpUrlConnection為基礎,故此認為有必要熟悉一下其基本用法。
使用的流程:
|
1 |
建立URL對象 |
URL url = new URL("http://qq.com"); |
|
2 |
執行個體化HttpUrlConnection對 |
conn = (HttpURLConnection) url.openConnection(); |
|
3 |
佈建要求的相關屬性,post傳值等 |
conn.setRequestMethod("POST");conn.setDoOutput()等 |
|
4 |
擷取返回碼,判斷串連成功與否 |
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) |
|
5 |
讀取輸入資料流 |
InputStream is = conn.getInputStream(); |
|
6 |
關閉串連、輸入資料流 |
conn.disconnect(); is.close(); |
Get請求:
//HttpUrlConnection預設就是Get請求,最簡單的情況下什麼都不需要設定。
conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
Get請求範例程式碼:
try { URL url = new URL("http://112.124.63.181/mm/Api/Public/timestamp"); conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } showText(sb.toString());} catch (IOException e) { e.printStackTrace();}finally { if (conn != null){conn.disconnect();} if (reader != null){ try { reader.close(); } catch (IOException e) {e.printStackTrace();} }}
Post請求:
Post和Get的主要區別:
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允許向伺服器提交資料
conn.setUseCaches(false);//Post不是用緩衝
String body ="password=e10adc3949ba59abbe56e057f20f883e&username=test3";
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
InputStream is = conn.getInputStream();
...
提交資料:
OutputStream os = conn.getOutputStream();
os.write(body.getBytes());
os.flush();
os.close();
直接使用低級位元組流輸出String轉換後的位元組數組。
DataOutputStream dos = new DataOutputStream(os);
OutputStreamWriter writer = new OutputStreamWriter(os);
BufferedWriter writer = new BufferedWriter(newOutputStreamWriter(os));
使用進階字元流可以相對比較方便地輸出字串、檔案等。
Post請求範例程式碼:
String urlString = "http://120.77.156.236/mm/Api/Base/checkLogin";String bodyString = "password=e10adc3949ba59abbe56e057f20f883e&username=test3";URL url = new URL(urlString);conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");OutputStream os = conn.getOutputStream();os.write(bodyString.getBytes("utf-8"));os.flush();os.close();if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString();}
常用設定方法:
// 佈建要求方法,預設是GET
conn.setRequestMethod("GET");
//設定串連伺服器逾時
conn.setConnectTimeout(8000);
//設定讀取伺服器資料逾時
conn.setReadTimeout(5000);
//設定是否使用緩衝
conn.setUseCaches(true);
// 設定使用的字元集
conn.setRequestProperty("Charset","UTF-8");
// 設定內容類型資訊
conn.setRequestProperty("Content-Type","application/json");
// 設定自訂參數
conn.setRequestProperty("MyKey","MyValue");
//設定是否允許輸出,預設為false,Post提交資料時必須為true
conn.setDoOutput
//設定是否允許輸入,預設為true,這樣getInputStream才有值
conn.setDoInput
//設定每次傳輸的流大小,防止手機記憶體不足
conn.setChunkedStreamingMode(51200);
//開始串連,並不需要顯式調用,調用getInputStream或getResponseCode等方式時都會間接調用。
conn.connect();
本文出自 “一劍圍城” 部落格,請務必保留此出處http://weijiancheng.blog.51cto.com/10190955/1928480
HttpUrlConnection發送GET、POST請求