android 發送http請求

來源:互聯網
上載者:User

標籤:下載   對象   dex   輸入   ted   第一行代碼   com   and   項目   

好久沒寫部落格了,由於公司要做android,筆者也是第一次接觸。

這是在項目中遇到一個比較麻煩的問題。記錄下來備忘(本人剛接觸。有不正確的地方請不吝賜教)。


發送請求的代碼:

package com.jiujian.mperdiem;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class AppUtil {     // 本地測試路徑  public static final String webBaseUrl = "http://ip:連接埠";  /*   * 訪問URL。擷取結果 method: GET, POST   */  public static String loadUrlResponse(String method, String urlString) {    HttpURLConnection conn = null; // 連線物件    InputStream is = null;    StringBuffer result = new StringBuffer();    try {      URL url = new URL(urlString); // URL對象      conn = (HttpURLConnection) url.openConnection(); // 使用URL開啟一個連結      conn.setDoInput(true); // 同意輸入資料流,即同意下載      conn.setDoOutput(true); // 同意輸出資料流,即同意上傳      conn.setUseCaches(false); // 不使用緩衝      conn.setRequestMethod(method); // 使用get請求      is = conn.getInputStream(); // 擷取輸入資料流。此時才真正建立連結      InputStreamReader isr = new InputStreamReader(is);      BufferedReader bufferReader = new BufferedReader(isr);      String inputLine = "";      while ((inputLine = bufferReader.readLine()) != null) {        result.append(inputLine).append("\n");      }    } catch (MalformedURLException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    } finally {      if (is != null) {        try {          is.close();        } catch (IOException e) {          e.printStackTrace();        }      }      if (conn != null) {        conn.disconnect();      }    }    return result.toString();  }}


調用代碼:

StringBuffer sbUpdateDeviceRefreshInstall = new StringBuffer(AppUtil.webBaseUrl);sbUpdateDeviceRefreshInstall.append("XXX?

UserId=");sbUpdateDeviceRefreshInstall.append(getUserId());AppUtil.loadUrlResponse("POST", sbUpdateDeviceRefreshInstall.toString());



代碼是沒有問題的,但是app端發送請求。server端卻一直沒有資訊列印。

錯誤資訊是:android.os.NetworkOnMainThreadException

最後才發現android 3.0以後就不同意在主線程上進行網路訪問的,

於是把代碼改成:

new Thread(){        @Override        public void run() {        StringBuffer sbUpdateDeviceRefreshInstall = new StringBuffer(AppUtil.webBaseUrl);        sbUpdateDeviceRefreshInstall.append("XXX?UserId="<span style="font-family: 'Microsoft YaHei';">);</span>        sbUpdateDeviceRefreshInstall.append(getUserId());        AppUtil.loadUrlResponse("POST", sbUpdateDeviceRefreshInstall.toString());        }        }.start();

這樣就沒問題了。


假設是剛接觸android,能夠推薦看:第一行代碼,這本書對於入門來說挺不錯的。

個人首頁:http://www.itit123.cn/ 很多其它乾貨等你來拿


android 發送http請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.