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

简介: 1、检测网络状态的代码 ConnectivityManager cm = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActivityNetworkInfo(); netInfo.toString(); 2.

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

目录
相关文章
|
4月前
|
XML JSON Java
Android App网络通信中通过okhttp调用HTTP接口讲解及实战(包括GET、表单格式POST、JSON格式POST 附源码)
Android App网络通信中通过okhttp调用HTTP接口讲解及实战(包括GET、表单格式POST、JSON格式POST 附源码)
146 0
|
2月前
|
JavaScript Java 数据安全/隐私保护
安卓逆向 -- POST数据解密
安卓逆向 -- POST数据解密
25 2
|
8月前
|
SQL 数据库 Android开发
Android 访问系统相册选中图片,并返回该图片的路径
Android 访问系统相册选中图片,并返回该图片的路径
95 0
|
4月前
|
缓存 Android开发 数据安全/隐私保护
android开发,使用kotlin学习HTTP访问网络
android开发,使用kotlin学习HTTP访问网络
69 0
|
4月前
|
XML Java Android开发
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)
94 0
|
4月前
|
XML Java Android开发
Android Studio App开发之网络通信中使用POST方式调用HTTP接口实现应用更新功能(附源码 超详细必看)
Android Studio App开发之网络通信中使用POST方式调用HTTP接口实现应用更新功能(附源码 超详细必看)
60 0
|
4月前
|
XML Java 定位技术
Android Studio App开发之网络通信中使用GET方式调用HTTP接口的讲解及实战(附源码 超详细必看)
Android Studio App开发之网络通信中使用GET方式调用HTTP接口的讲解及实战(附源码 超详细必看)
56 0
|
6月前
Android-async-http 添加token get方法报错 No valid URI scheme was provided
Android-async-http 添加token get方法报错 No valid URI scheme was provided
|
8月前
|
Java Android开发
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
51 0
|
10月前
|
存储 XML JSON
Android配置文件操作模块封装,全互联网最简单好用的封装
Android配置文件操作模块封装,全互联网最简单好用的封装