您好,登錄后才能下訂單哦!
之前在使用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();
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。