Android開發(fā)中的幾種網(wǎng)絡(luò)請求方式詳解
Android應(yīng)用經(jīng)常會和服務(wù)器端交互,這就需要手機(jī)客戶端發(fā)送網(wǎng)絡(luò)請求,下面介紹四種常用網(wǎng)絡(luò)請求方式,我這邊是通過Android單元測試來完成這四種方法的,還不清楚Android的單元測試的同學(xué)們請看Android開發(fā)技巧總結(jié)中的Android單元測試的步驟一文。
Java.NET包中的HttpURLConnection類
Get方式:
// Get方式請求
public static void requestByGet() throws Exception {
String path = "http://www.dbjr.com.cn/logins.jsp?id=helloworld&pwd=android";
// 新建一個URL對象
URL url = new URL(path);
// 打開一個HttpURLConnection連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設(shè)置連接超時時間
urlConn.setConnectTimeout(5 * 1000);
// 開始連接
urlConn.connect();
// 判斷請求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_GET, "Get方式請求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_GET, new String(data, "UTF-8"));
} else {
Log.i(TAG_GET, "Get方式請求失敗");
}
// 關(guān)閉連接
urlConn.disconnect();
}
Post方式:
// Post方式請求
public static void requestByPost() throws Throwable {
String path = "http://www.dbjr.com.cn/logins.jsp";
// 請求的參數(shù)轉(zhuǎn)換為byte數(shù)組
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();
// 設(shè)置連接超時時間
urlConn.setConnectTimeout(5 * 1000);
// Post請求必須設(shè)置允許輸出
urlConn.setDoOutput(true);
// Post請求不能使用緩存
urlConn.setUseCaches(false);
// 設(shè)置為Post請求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置請求Content-Type
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 開始連接
urlConn.connect();
// 發(fā)送請求參數(shù)
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判斷請求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_POST, "Post請求方式成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_POST, new String(data, "UTF-8"));
} else {
Log.i(TAG_POST, "Post方式請求失敗");
}
}
org.apache.http包中的HttpGet和HttpPost類
Get方式:
// HttpGet方式請求
public static void requestByHttpGet() throws Exception {
String path = "http://www.dbjr.com.cn/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet對象
HttpGet httpGet = new HttpGet(path);
// 獲取HttpClient對象
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpGet);
// 判斷是夠請求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpGet方式請求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpGet方式請求失敗");
}
}
Post方式:
// HttpPost方式請求
public static void requestByHttpPost() throws Exception {
String path = "http://www.dbjr.com.cn/";
// 新建HttpPost對象
HttpPost httpPost = new HttpPost(path);
// Post參數(shù)
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "helloworld"));
params.add(new BasicNameValuePair("pwd", "android"));
// 設(shè)置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 設(shè)置參數(shù)實(shí)體
httpPost.setEntity(entity);
// 獲取HttpClient對象
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判斷是夠請求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpPost方式請求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpPost方式請求失敗");
}
}
以上是一些部分代碼,測試的時候在測試類中運(yùn)行對應(yīng)的測試方法即可。完整代碼點(diǎn)這里下載:源碼下載
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitte
最近使用android10創(chuàng)建文件失敗,并拋出權(quán)限異常,這篇文章主要給大家介紹了Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitted異常的解決方法,需要的朋友可以參考下2023-05-05
Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動
這篇文章主要介紹了Android開發(fā)之ProgressBar字體隨著進(jìn)度條的加載而滾動,需要的朋友可以參考下2017-09-09
Android Flutter實(shí)戰(zhàn)之為照片添加顏色濾鏡
這篇文章我們將利用TweenAnimationBuilder來實(shí)現(xiàn)一個圖片調(diào)色的過渡動畫,從而實(shí)現(xiàn)為照片添加顏色濾鏡的效果,感興趣的可以了解一下2022-12-12
Android 錢包支付之輸入支付密碼的實(shí)現(xiàn)步驟
這篇文章主要介紹了Android 錢包支付之輸入支付密碼,需要的朋友可以參考下2018-04-04
Flutter插件開發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter插件開發(fā)之HmsScanKit實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android自定義View實(shí)現(xiàn)打鉤動畫功能
本篇文章通過實(shí)例給大家分享了Android自定義View實(shí)現(xiàn)打鉤動畫功能的過程和代碼分享,有興趣需要的學(xué)習(xí)下吧。2017-12-12
Android編程實(shí)現(xiàn)禁止系統(tǒng)鎖屏與解鎖亮屏的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)禁止系統(tǒng)鎖屏與解鎖亮屏的方法,實(shí)例分析了Android關(guān)閉屏幕、鎖屏及解鎖屏幕的相關(guān)技巧,需要的朋友可以參考下2015-12-12

