android 访问网络二 (基于httpurlconnection post)
来源:互联网 发布:2016免费翻墙软件 编辑:程序博客网 时间:2024/06/16 09:50
1、网络请求中,post方式请求的参数是在请求体中带给服务器端的 并且需要设置内容与长度的头信息
下面一个简单登录示例:
String path = "http://192.168.1.96:8080/Loginfromphone/login"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
<span style="white-space:pre"></span>//设置可以用服务器端写数据conn.setDoOutput(true);
<span style="white-space:pre"></span>//设置链接超时conn.setConnectTimeout(5000);
<span style="white-space:pre"></span>//post请求方式conn.setRequestMethod("POST"); // Content-Type: application/x-www-form-urlencodedconn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");<span style="white-space:pre"></span>String content = "username="+ URLEncoder.encode(name, "utf-8") + "&password="+ URLEncoder.encode(pwd, "utf-8");<span style="white-space:pre"></span>// Content-Length: 20conn.setRequestProperty("Content-Length", content.length()+ "");conn.getOutputStream().write(content.getBytes("utf-8"));int code = conn.getResponseCode();if (code == 200) {InputStream in = conn.getInputStream();String data = StreamTool.stream2String(in);Message msg = Message.obtain();msg.what = SUCEESS;msg.obj = data;handler.sendMessage(msg);} else {handler.sendEmptyMessage(ERROR);}
<span style="white-space:pre"></span>private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case SUCEESS:Toast.makeText(MainActivity.this, (String) msg.obj, 0).show();break;case ERROR:Toast.makeText(MainActivity.this, "请求有误", 0).show();break;case NET_ERROR:Toast.makeText(MainActivity.this, "网络异常", 0).show();break;default:break;}};};
0 0
- android 访问网络二 (基于httpurlconnection post)
- Android HttpURLConnection get、post 访问网络
- android HttpURLConnection网络访问
- android 中访问网络介绍 一 (基于httpurlconnection 的中get请求)
- Android网络编程(二)HttpURLConnection
- Android 之 HttpURLConnection 访问网络
- Android 用HttpURLConnection访问网络
- Android基于HttpURLConnection网络请求
- 基于HttpURLConnection的网络访问工具
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- android网络编程之HttpUrlConnection--POST请求
- Android网络通信技术之基于HttpURLConnection的POST/GET传值问题
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- http://blog.sina.com.cn/s/blog_a3d2fd2d0101hyu7.html
- HDU1506
- 逆序数的个数利用归并排序递归解决
- PIL 中的 Image 模块
- android 二维码 google ZXing
- android 访问网络二 (基于httpurlconnection post)
- Java语言基础及特性-01
- 嵌入式软件开发培训笔记——boot原理与u-boot 基于Cortex_A8核的S5PC100芯片
- 关于Error: xz compression not available
- JSR 303 - Bean Validation 介绍及最佳实践
- tomcat启动报错:Address already in use: JVM_Bind(端口冲突)
- 编译生成ARM上可运行的Qt程序
- 嵌入式软件开发培训笔记——嵌入式linux内核与文件系统移植与分析
- C语言产生随机数