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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android互联网访问,get方式,post方式等方式

發布時間:2024/9/27 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android互联网访问,get方式,post方式等方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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));

//必須設置內容類型,否則服務器無法識別數據類型.

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

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

outStream.write(xmlbyte);//發送xml數據

outStream.flush();

總結

以上是生活随笔為你收集整理的Android互联网访问,get方式,post方式等方式的全部內容,希望文章能夠幫你解決所遇到的問題。

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