欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法

 更新時(shí)間:2018年01月30日 09:27:58   作者:待風(fēng)  
下面小編就為大家分享一篇Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

一、 HttpURLConnection以GET方式訪問網(wǎng)絡(luò):

HttpURLConnection connection = null;
try {
 URL url = new URL("https://www.xxx.com/");
 connection = (HttpURLConnection) url.openConnection();
 connection.setRequestMethod("GET");//設(shè)置訪問方式為“GET”
 connection.setConnectTimeout(8000);//設(shè)置連接服務(wù)器超時(shí)時(shí)間為8秒
 connection.setReadTimeout(8000);//設(shè)置讀取服務(wù)器數(shù)據(jù)超時(shí)時(shí)間為8秒

 if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
  //從服務(wù)器獲取響應(yīng)并把響應(yīng)數(shù)據(jù)轉(zhuǎn)為字符串打印
  InputStream in = connection.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(in));

  StringBuilder response = new StringBuilder();
  String line;
  while (null != (line = reader.readLine())) {
    response.append(line);
  }
  Log.d(TAG, response.toString());
 }
} catch (Exception e) {
 e.printStackTrace();
} finally {
 if (null!= connection) {
   connection.disconnect();
 }
}

二、 HttpURLConnection以POST方式訪問網(wǎng)絡(luò):

HttpURLConnection connection = null;
  try{
   URL url = new URL("https://www.xxx.com/");
   connection = (HttpURLConnection) url.openConnection();

   connection.setRequestMethod("POST");
   connection.setConnectTimeout(8000);
   connection.setReadTimeout(8000);
   connection.setDoOutput(true);// 使用 URL 連接進(jìn)行輸出
   connection.setDoInput(true);// 使用 URL 連接進(jìn)行輸入
   connection.setUseCaches(false);// 忽略緩存

   // 建立輸出流,并寫入數(shù)據(jù)
   OutputStream outputStream = connection.getOutputStream();
   DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
   dataOutputStream.writeBytes("username=admin&password=888888");
   dataOutputStream.close();

   if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
    // 當(dāng)正確響應(yīng)時(shí)處理數(shù)據(jù)
    StringBuffer response = new StringBuffer();
    String line;
    BufferedReader responseReader = 
      new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
    // 處理響應(yīng)流,必須與服務(wù)器響應(yīng)流輸出的編碼一致
    while (null != (line = responseReader.readLine())) {
     response.append(line);
    }
    responseReader.close();
    Log.d(TAG, response.toString());
   }

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (null!= connection) {
    connection.disconnect();
   }
  }

注意:

1. HTTP訪問是不允許在主線程進(jìn)行的,否則會(huì)報(bào)錯(cuò)。因此上面的操作應(yīng)該在新線程中進(jìn)行。

2. 一般要用HttpURLConnection.getResponseCode() == 200來判斷是否正常響應(yīng)。為true則正常響應(yīng)。

3. 在Android 2.2及以下版本,使用的是HttpClient,Android 2.3及以上版本,使用的是HttpURLConnection,而Android5.1之后廢棄了HttpClient的相關(guān)Api。因此HttpClient用法不再進(jìn)行研究。

4. 以POST方式提交數(shù)據(jù)時(shí),每條數(shù)據(jù)要以鍵值對(duì)的方式提交,各條數(shù)據(jù)之間以&隔開。比如上面的代碼中dataOutputStream.writeBytes(“username=admin&password=888888”);

5. 上面用到了StringBuilder和StringBuffer,沒有什么特別用意,只是順便用下。StringBuilder在單線程下比StringBuffer更高效,但不是線程安全的。

以上這篇Android 用HttpURLConnection訪問網(wǎng)絡(luò)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論