Android中的幾種網路請求方式詳解
Android應用經常會和伺服器端互動,這就需要手機客戶端傳送網路請求,下面介紹四種常用網路請求方式,我這邊是通過Android單元測試來完成這四種方法的,還不清楚Android的單元測試的同學們請看Android開發技巧總結中的Android單元測試的步驟一文。
java.net包中的HttpURLConnection類
Get方式:
// Get方式請求
public static void requestByGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建一個URL物件
URL url = new URL(path);
// 開啟一個HttpURLConnection連線
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設定連線超時時間
urlConn.setConnectTimeout(5 * 1000);
// 開始連線
urlConn.connect();
// 判斷請求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的資料
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_GET, "Get方式請求成功,返回資料如下:");
Log.i(TAG_GET, new String(data, "UTF-8"));
} else {
Log.i(TAG_GET, "Get方式請求失敗");
}
// 關閉連線
urlConn.disconnect();
}
Post方式:
// Post方式請求
public static void requestByPost() throws Throwable {
String path = "https://reg.163.com/logins.jsp";
// 請求的引數轉換為byte陣列
String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
byte[] postData = params.getBytes();
// 新建一個URL物件
URL url = new URL(path);
// 開啟一個HttpURLConnection連線
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設定連線超時時間
urlConn.setConnectTimeout(5 * 1000);
// Post請求必須設定允許輸出
urlConn.setDoOutput(true);
// Post請求不能使用快取
urlConn.setUseCaches(false);
// 設定為Post請求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置請求Content-Type
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 開始連線
urlConn.connect();
// 傳送請求引數
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判斷請求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的資料
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_POST, "Post請求方式成功,返回資料如下:");
Log.i(TAG_POST, new String(data, "UTF-8"));
} else {
Log.i(TAG_POST, "Post方式請求失敗");
}
}
org.apache.http包中的HttpGet和HttpPost類
// HttpGet方式請求
public static void requestByHttpGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet物件
HttpGet httpGet = new HttpGet(path);
// 獲取HttpClient物件
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse例項
HttpResponse httpResp = httpClient.execute(httpGet);
// 判斷是夠請求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的資料
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpGet方式請求成功,返回資料如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpGet方式請求失敗");
}
}
//
HttpPost方式請求
public static void requestByHttpPost() throws Exception {
String path = "https://reg.163.com/logins.jsp";
// 新建HttpPost物件
HttpPost httpPost = new HttpPost(path);
// Post引數
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "helloworld"));
params.add(new BasicNameValuePair("pwd", "android"));
// 設定字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 設定引數實體
httpPost.setEntity(entity);
// 獲取HttpClient物件
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse例項
HttpResponse httpResp = httpClient.execute(httpPost);
// 判斷是夠請求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的資料
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpPost方式請求成功,返回資料如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpPost方式請求失敗");
}
}
相關文章
- Android小知識-剖析Retrofit中網路請求的兩種方式Android
- php下請求url的幾種方式PHP
- js 幾種網路請求方式梳理——擺脫回撥地獄JS
- Android網路請求(4) 網路請求框架VolleyAndroid框架
- Android網路請求(終) 網路請求框架RetrofitAndroid框架
- Android網路請求(3) 網路請求框架OkHttpAndroid框架HTTP
- Android網路請求(2)Android
- 全面分析前端的網路請求方式前端
- VMware連線網路的幾種方式
- 51. ajax幾種請求方式?他們的優缺點?
- 輕鬆搞定Retrofit不同網路請求方式的請求引數配置,及常用註解使用
- 輕鬆搞定Retrofit不同網路請求方式的請求引數配置,Retrofit常用註解的使用
- android 網路請求時,顯示progrossBarAndroidROS
- 談談axios中Post請求變成OPTIONS的幾種解決方案iOS
- 【詳細】關於Android上傳檔案的幾種方式Android
- Jest中Mock網路請求Mock
- Java中實現並行請求兩種方式Java並行
- 在lua中操作http請求有兩種方式HTTP
- Android中取消系統標題欄的幾種方式Android
- Flutter 網路請求的三種簡單實現Flutter
- 前後端資料互動(一)——網路請求詳解後端
- Linux幾種轉發請求方案Linux
- 盤點 Spring Boot 解決跨域請求的幾種方法Spring Boot跨域
- Python多種介面請求方式示例Python
- JS 中的網路請求 AJAX, Fetch, WebSocketJSWeb
- POSTMAN HTTP請求的四種方式區別PostmanHTTP
- curl 傳送 POST 請求的四種方式
- 網路安全中常用的幾種加密方式都有哪些?加密
- Android 截圖實現的幾種方式Android
- Android生成ViewModel例項的幾種方式AndroidView
- Springboot -- 用更優雅的方式發HTTP請求(RestTemplate詳解)Spring BootHTTPREST
- Spring Boot中的 6 種API請求引數讀取方式Spring BootAPI
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- Java中陣列判斷元素存在幾種方式比較詳解Java陣列
- 網路請求了
- Android幾種非同步方式,解決主執行緒中遇到的卡頓Android非同步執行緒
- 網路請求優化之取消請求優化
- 幾種常見網路抓包方式介紹
- axios(xhr) 和 fetch 兩種請求方式iOS