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

目录
相关文章
|
10月前
|
Android开发
uniapp 原生android插件实现get和post请求
uniapp 原生android插件实现get和post请求
178 0
uniapp 原生android插件实现get和post请求
|
Android开发 机器学习/深度学习
|
XML Android开发 数据格式
【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
<h1>备份短信和添加短信</h1> <p></p> <p>操作系统短信的uri: content://sms/</p> <pre code_snippet_id="1644588" snippet_file_name="blog_20160413_1_3407084" name="code" class="html">&lt;?xml version="1.0" encoding=
2179 0
Android--httpclient模拟post请求和get请求
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/50964727 HttpClient的使用模式: 1.
882 0
|
Android开发 网络协议 存储
Android客户端采用Http 协议Post方式请求与服务端进行数据交互
本示例以Servlet为例,演示Android与Servlet的通信。 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式。
1412 0
相关产品
云迁移中心
推荐文章
更多