国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解Android開發中的幾種網絡請求方式

2020-02-21 17:30:16
字體:
來源:轉載
供稿:網友

Android應用程序經常與服務器端交互,這需要移動客戶端發送網絡請求,以下是常見的網絡請求方式,武林技術頻道小編詳解Android開發中的幾種網絡請求方式,希望對你學習有幫助!

Java.NET包中的HttpURLConnection類

Get方式:

// Get方式請求 public static void requestByGet() throws Exception {  String path = "http://m.survivalescaperooms.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 = "http://m.survivalescaperooms.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類

Get方式:

// HttpGet方式請求 public static void requestByHttpGet() throws Exception {  String path = "http://m.survivalescaperooms.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方式請求失敗");  } } 

Post方式:

// HttpPost方式請求 public static void requestByHttpPost() throws Exception {  String path = "http://m.survivalescaperooms.com/";  // 新建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開發中的幾種網絡請求方式,新手朋友們一定要好好閱讀,喜歡的話也可以幫忙分享出去哦。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昆明市| 富宁县| 抚顺市| 毕节市| 克东县| 石城县| 武义县| 汾西县| 来宾市| 英吉沙县| 通河县| 科技| 西城区| 彰武县| 邯郸县| 吉林省| 孙吴县| 景德镇市| 青铜峡市| 贺州市| 历史| 油尖旺区| 鄂伦春自治旗| 铅山县| 洛川县| 兴安盟| 滕州市| 彰武县| 临潭县| 乃东县| 绵阳市| 梅州市| 巧家县| 珠海市| 揭东县| 凤阳县| 常德市| 泽库县| 新化县| 民勤县| 砚山县|