Android互連網訪問,get方式,post方式等方式

來源:互聯網
上載者:User

1、檢測網路狀態的代碼

ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = cm.getActivityNetworkInfo();

netInfo.toString();

2.使用網路許可權

<uses-permission android:name="android.permission.INTERNET"/>

3.通過URL + HttpURLConnection獲得資料(文本和圖片)

URL url = new URL("http://www.sohu.com");

HttpURLConnection conn = (HttpURLConnectioni) url.openConnection();

conn.setConnectionTimeout(5*1000);

conn.setRequestMethod("GET");

conn.getResponseMethod("GET");

(conn.getResponseCode() != 200 ) throw...;

InputStream is = conn.getInputStream();

String result = readData(is,"GEK");

conn.disconnect();

//擷取圖片同上

1.GET方式發送name-value對

//http://xxxx/xxx.action?name=tom&&age=12

URL realUrl = new URL(requestUrl);

HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();

conn.setRequestMethod("GET");

conn.setConnectionTimeout(5000);        

//直到此時,資料才發往伺服器

if( conn.getResponseCode() == 200 ){

String result = readAsString(conn.getInputStream(), "UTF-8");

outStream.close();

System.out.println(result);

}

注:中文亂碼問題.

//utf-8指伺服器端編碼

1.get方式發送中文需要轉碼.UrlEncode("中文","utf-8");

2.tomcat器端需要轉碼(get方式).

if("GET".equals(request.getMethod())){

String v  =request.getParameter("name");

  new String(v.getBytes("ISO8859-1"),"UTF-8");

}

1.POST發送普通常值內容

URL realUrl = new URL(requestUrl);

HttpURLConncection conn = (HttpURLConnection) realUrl.openConnection();

conn.setDoOutput(true);

conn.setUseCaches(false);

conn.setUseCaches(false);

conn.setRequestMethod();

conn.setRequestProperty("Connection","Keep-Alive");//維持長串連

conn.setRequestProperty("Charset","UTF-8");

con.setRequestProperty("Content-Length", String.valueOf(data.length));

con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

String str = "name=xxx&age=12";

con.getOutputStream().write(str.getBytes);

If(conn.getOutputStream().write(Str.getBytes)){

if(conn.getResponseCode()==200){

String result = readAsString(conn.getInputStream(),"UTF-8");

outStream.close();

System.out.println(result):

}

}

1.POST發送xml

StringBuilder xml =  new StringBuilder();

xml.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");

xml.append("<M1 V=10000>");

xml.append("<U I=1 D=\"N73\">中國</U>");

xml.append("</M1>");

byte[] xmlbyte = xml.toString().getBytes("UTF-8");

URL url = new URL("http://xxx.do?method=readxml");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5* 1000);

conn.setDoOutput(true);//允許輸出

conn.setUseCaches(false);//不使用Cache

conn.setRequestMethod("POST");        

conn.setRequestProperty("Connection", "Keep-Alive");//維持長串連

conn.setRequestProperty("Charset", "UTF-8");

conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));

//必須設定內容類型,否則伺服器無法識別資料類型.www.2cto.com

conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");

DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());

outStream.write(xmlbyte);//發送xml資料

outStream.flush();
作者:toto1297488504

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.