日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 请求方式有哪些,Android中的几种网络请求方式详解

發布時間:2023/11/27 Android 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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() throwsThrowable {

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 = newURL(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 = newDataOutputStream(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類

Get方式:

//HttpGet方式請求

public static void requestByHttpGet() throwsException {

String path= "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";//新建HttpGet對象

HttpGet httpGet = newHttpGet(path);//獲取HttpClient對象

HttpClient httpClient = newDefaultHttpClient();//獲取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方式請求失敗");

}

}

Post方式:

//HttpPost方式請求

public static void requestByHttpPost() throwsException {

String path= "https://reg.163.com/logins.jsp";//新建HttpPost對象

HttpPost httpPost = newHttpPost(path);//Post參數

List params = new ArrayList();

params.add(new BasicNameValuePair("id", "helloworld"));

params.add(new BasicNameValuePair("pwd", "android"));//設置字符集

HttpEntity entity = newUrlEncodedFormEntity(params, HTTP.UTF_8);//設置參數實體

httpPost.setEntity(entity);//獲取HttpClient對象

HttpClient httpClient = newDefaultHttpClient();//獲取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 请求方式有哪些,Android中的几种网络请求方式详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

歡迎分享!

轉載請說明來源于"生活随笔",并保留原作者的名字。

本文地址:android 请求方式有哪些,Android中的几种网络请