Android開發(fā)中的幾種網(wǎng)絡(luò)請(qǐng)求方式詳解
Android應(yīng)用經(jīng)常會(huì)和服務(wù)器端交互,這就需要手機(jī)客戶端發(fā)送網(wǎng)絡(luò)請(qǐng)求,下面介紹四種常用網(wǎng)絡(luò)請(qǐng)求方式,我這邊是通過Android單元測(cè)試來(lái)完成這四種方法的,還不清楚Android的單元測(cè)試的同學(xué)們請(qǐng)看Android開發(fā)技巧總結(jié)中的Android單元測(cè)試的步驟一文。
Java.NET包中的HttpURLConnection類
Get方式:
// Get方式請(qǐng)求
public static void requestByGet() throws Exception {
String path = "http://www.dhdzp.com/logins.jsp?id=helloworld&pwd=android";
// 新建一個(gè)URL對(duì)象
URL url = new URL(path);
// 打開一個(gè)HttpURLConnection連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設(shè)置連接超時(shí)時(shí)間
urlConn.setConnectTimeout(5 * 1000);
// 開始連接
urlConn.connect();
// 判斷請(qǐng)求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_GET, "Get方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_GET, new String(data, "UTF-8"));
} else {
Log.i(TAG_GET, "Get方式請(qǐng)求失敗");
}
// 關(guān)閉連接
urlConn.disconnect();
}
Post方式:
// Post方式請(qǐng)求
public static void requestByPost() throws Throwable {
String path = "http://www.dhdzp.com/logins.jsp";
// 請(qǐng)求的參數(shù)轉(zhuǎn)換為byte數(shù)組
String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
byte[] postData = params.getBytes();
// 新建一個(gè)URL對(duì)象
URL url = new URL(path);
// 打開一個(gè)HttpURLConnection連接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 設(shè)置連接超時(shí)時(shí)間
urlConn.setConnectTimeout(5 * 1000);
// Post請(qǐng)求必須設(shè)置允許輸出
urlConn.setDoOutput(true);
// Post請(qǐng)求不能使用緩存
urlConn.setUseCaches(false);
// 設(shè)置為Post請(qǐng)求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置請(qǐng)求Content-Type
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 開始連接
urlConn.connect();
// 發(fā)送請(qǐng)求參數(shù)
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判斷請(qǐng)求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_POST, "Post請(qǐng)求方式成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_POST, new String(data, "UTF-8"));
} else {
Log.i(TAG_POST, "Post方式請(qǐng)求失敗");
}
}
org.apache.http包中的HttpGet和HttpPost類
Get方式:
// HttpGet方式請(qǐng)求
public static void requestByHttpGet() throws Exception {
String path = "http://www.dhdzp.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet對(duì)象
HttpGet httpGet = new HttpGet(path);
// 獲取HttpClient對(duì)象
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpGet);
// 判斷是夠請(qǐng)求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpGet方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpGet方式請(qǐng)求失敗");
}
}
Post方式:
// HttpPost方式請(qǐng)求
public static void requestByHttpPost() throws Exception {
String path = "http://www.dhdzp.com/";
// 新建HttpPost對(duì)象
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對(duì)象
HttpClient httpClient = new DefaultHttpClient();
// 獲取HttpResponse實(shí)例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判斷是夠請(qǐng)求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 獲取返回的數(shù)據(jù)
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpPost方式請(qǐng)求成功,返回?cái)?shù)據(jù)如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpPost方式請(qǐng)求失敗");
}
}
以上是一些部分代碼,測(cè)試的時(shí)候在測(cè)試類中運(yùn)行對(duì)應(yīng)的測(cè)試方法即可。完整代碼點(diǎn)這里下載:源碼下載
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android Xutils3網(wǎng)絡(luò)請(qǐng)求的封裝詳解及實(shí)例代碼
- Android GZip的使用-開發(fā)中網(wǎng)絡(luò)請(qǐng)求的壓縮實(shí)例詳解
- android 網(wǎng)絡(luò)請(qǐng)求庫(kù)volley方法詳解
- Android HTTP網(wǎng)絡(luò)請(qǐng)求的異步實(shí)現(xiàn)
- Android最基本的異步網(wǎng)絡(luò)請(qǐng)求框架
- Android網(wǎng)絡(luò)請(qǐng)求庫(kù)android-async-http介紹
- Android基于OkHttpUtils網(wǎng)絡(luò)請(qǐng)求的二次封裝
相關(guān)文章
Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)(上)分組導(dǎo)航和擠壓動(dòng)畫(附源碼)
本文將為大家講解下Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)之分組導(dǎo)航和擠壓動(dòng)畫,具體實(shí)現(xiàn)及源代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家學(xué)習(xí)有所幫助2013-06-06
Android權(quán)限機(jī)制帶來(lái)的一些安全問題介紹
這篇文章主要介紹了Android權(quán)限機(jī)制帶來(lái)的一些安全問題介紹,本文講解了權(quán)限機(jī)制的缺陷和不足、樹立權(quán)限意識(shí)、越過權(quán)限機(jī)制等內(nèi)容,需要的朋友可以參考下2015-04-04
Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿iphone抖動(dòng)效果的方法,結(jié)合實(shí)例形式分析了仿iphone抖動(dòng)效果的頁(yè)面布局及功能實(shí)現(xiàn)技巧,并附帶實(shí)例源碼供讀者下載,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法
這篇文章主要介紹了Android實(shí)現(xiàn)EditText內(nèi)容保存為Bitmap的方法,涉及Android中saveBitmap方法的簡(jiǎn)單使用技巧,需要的朋友可以參考下2016-01-01
Android App開發(fā)中ViewPager組件的入門使用教程
這篇文章主要介紹了Android App開發(fā)中ViewPager組件的入門使用教程,ViewPager主要用來(lái)實(shí)現(xiàn)通過滑動(dòng)來(lái)切換頁(yè)面的效果,需要的朋友可以參考下2016-03-03
Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)分享功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)分享功能,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)針對(duì)文字、圖片等元素分享功能的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
flutter實(shí)現(xiàn)倒計(jì)時(shí)加載頁(yè)面
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)倒計(jì)時(shí)加載頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Kotlin自定義實(shí)現(xiàn)支付密碼數(shù)字鍵盤的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Kotlin如何自定義實(shí)現(xiàn)支付密碼數(shù)字鍵盤的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁(yè)設(shè)置進(jìn)度條的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

