HttpUrlConnection用get和post请求发送参数
来源:互联网 发布:tabu search算法 编辑:程序博客网 时间:2024/06/16 23:31
post请求:
final String nameValue = username.getText().toString(); final String passValue = password.getText().toString(); new Thread(){ public void run() { try { URL url = new URL(login_url); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); urlConn.setConnectTimeout(5*1000);//设置连接时间为5秒 urlConn.setReadTimeout(5*1000);//设置读取时间为5秒 urlConn.setRequestMethod("POST");//设置请求方式为post urlConn.setDoOutput(true); urlConn.setDoInput(true); //添加参数 OutputStream outputStream = urlConn.getOutputStream(); String data = "username="+nameValue+"&password="+passValue;//拼装参数 outputStream.write(data.getBytes());//上传参数 int code = urlConn.getResponseCode(); if(code == 200){//相应成功,获得相应的数据 InputStream is = urlConn.getInputStream();//得到数据流(输入流) byte[] buffer = new byte[1024]; int length = 0; String str = ""; while((length = is.read(buffer)) > -1){ str += new String(buffer,0,length); } Log.d("main", str); //解析json,展示在ListView(GridView) //h.sendMessage(h.obtainMessage(3, str)); LoginResult lr = new Gson().fromJson(str, LoginResult.class); if(lr.getCode() == 1){ //可以本地保存服务器发送过来的完整的账号信息 User user = lr.getUser();//得到账号的完整信息(从服务器发送过来) String header = user.getHeader();//得到头像url Intent it = new Intent(MainActivity.this,SuccessActivity.class); it.putExtra("headerUrl", header); startActivity(it); }else{ Toast.makeText(MainActivity.this, "登陆失败,请重新输入", 0).show(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
get请求
login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final String nameValue = username.getText().toString(); final String passValue = password.getText().toString(); //访问服务器 new Thread(){ public void run() { //拼装url //URLEncoder.encode对汉字进行编码,服务器进行解码设置,解决中文乱码 try { String lastUrl = login_url + "?username="+URLEncoder.encode(nameValue, "utf-8")+"&password="+passValue; URL url = new URL(lastUrl); HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();//开发访问此连接 //设置访问时长和相应时长 urlConn.setConnectTimeout(5*1000);//设置连接时间为5秒 urlConn.setReadTimeout(5*1000);//设置读取时间为5秒 int code = urlConn.getResponseCode();//获得相应码 if(code == 200){//相应成功,获得相应的数据 InputStream is = urlConn.getInputStream();//得到数据流(输入流) byte[] buffer = new byte[1024]; int length = 0; String data = ""; while((length = is.read(buffer)) != -1){ String str = new String(buffer,0,length); data += str; } Log.d("main", data); //解析json,展示在ListView(GridView) h.sendMessage(h.obtainMessage(2, data)); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); }; });
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
网络请求----HttpURLConnection的get,post和图片加载
- 2016-12-05 14:28
- 242KB
- 下载
HttpURLConnection 向服务器发送post和get请求 并接收响应
HttpURLConnection 向服务器发送post和get请求 并接收响应- Dopamy_BusyMonkey
- 2016-03-11 10:39
- 1156
月薪30k的前端程序员都避开了哪些坑?
程序员薪水有高有低,同样工作5年的程序员,有的人月薪30K、50K,有的人可能只有5K、8K。是什么因素导致了这种差异?
使用HttpURLConnection向服务器发送post和get请求
HttpConnection的请求方式主要有两种,get和post,post请求和get请求的最大不同就是提交请求信息的方式,post是通过把请求信息封装在http请求头中发送出去的,get请求是把请...- ljw124213
- 2016-08-22 20:27
- 1823
使用HttpURLConnection向服务器发送post和get请求
一、使用HttpURLConnection向服务器发送get请求1、向服务器发送get请求 @Test publicvoid sendSms() throws Excep...- u012777182
- 2016-04-10 19:17
- 195
使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- wzq9706
- 2012-02-08 10:09
- 558
Http学习之使用HttpURLConnection发送post和get请求 .
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- m372897500
- 2012-08-17 17:54
- 466
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- deaboway
- 2011-02-23 14:08
- 868
使用HttpURLConnection发送get和post请求
转载请注明出处:我们在开发的使用,直接使用的开源框架,例如:Xutil,Volley开源框架直接访问网络,但是我们也需要知道其中的一些知识,了解一下怎样访问网络的。下面我们模拟以下客户端和服务端,看...- forwardyzk
- 2015-04-29 11:35
- 2996
Http学习之使用HttpURLConnection发送post和get请求
Http学习之使用HttpURLConnection发送post和get请求最常用的Http请求无非是get 和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给serv...- MAZHEN1986
- 2012-04-24 14:10
- 249
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- RamondLee
- 2011-08-11 17:27
- 445
Android开发点点滴滴——使用HttpURLConnection发送get和post请求
android和PC通信,http方式是必不可少的,如何使用android来实现发送http请求,并且得到http的回复消息呢?可以使用HttpURLConnection,来实现:1.发送get请...- zhd320
- 2013-10-29 16:19
- 836
HttpURLConnection发送post和get请求
http://blog.csdn.net/chenlei1889/article/details/6363356 最常用的Http请求无非是get和post,get请求可以获取静态页面,也...- crazy_fire
- 2012-06-11 16:13
- 636
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- tomorrow_fine
- 2016-11-10 13:10
- 153
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- w112121
- 2013-11-17 20:19
- 1043
Http学习之使用HttpURLConnection发送post和get请求
最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...- luckarecs
- 2012-06-27 11:12
- 3618
使用HttpURLConnection发送post和get请求
使用HttpURLConnection发送post和get请求1、http://blog.csdn.net/pandazxx/archive/2007/06/18/1657109.aspx2、http...- hitmediaman
- 2011-03-20 10:04
- 446
Android例子—HttpURLConnection发送POST、GET请求代码示例
这里我们主要针对GET和POST请求写两个不同的使用示例,我们可以conn.getInputStream() 获取到的是一个流,所以我们需要写一个类将流转化为二进制数组!工具类如下:StreamToo...- u012758088
- 2017-04-05 21:51
- 94
Android 使用HttpURLConnection发送Post/Get请求
HTTP规范定义中最常用的请求类型就是Get和Post。当你在浏览器里输入任意一个网址按回车,浏览器即已经在执行Get请求了;当你回复了某条微博时,这时可能就执行了一次Post请求。简单的来说,Get...- GeekSpirit
- 2013-03-21 17:35
- 1595
Http学习之使用HttpURLConnection发送post和get请求
转自 http://blog.csdn.net/pandazxx/article/details/1657109最常用的Http请求无非是get和post,get请求可以获取静态页面...- zws1987211
- 2011-10-09 16:34
- 364
HttpURLConnection发送Get和Post请求
HttpURLConnection是java的标准类,可发送get请求和post请求。关于Get和Post的区别,这里就不细说了,网上普遍的说法是:1、GET请求是从服务器上获取数据,POST请求...- u013933272
- 2016-05-17 18:07
- 4366
- 原创
- 23
- 粉丝
- 106
- 喜欢
- 0
- 码云
- 未开通
他的最新文章
更多文章- Banner轮播图的使用
- Tablayout的基础使用
- DrawerLayout的使用
- XListView请求网络数据
在线课程
【福利】神经网络的原理及结构设计
讲师:
互联网职业选择那点事
讲师:
热门文章
- java中单引号和双引号有什么区别?1970
- Android启动页停留2秒跳转到主页或引导页的最好好实现1534
- 可以获取文件扩展名的函数,形参接收一个文件名字符串,返回一个扩展名字符串。352
- android 常用权限和依赖351
- Android 5.0新的特性对开发者会有什么影响338
阅读全文
0 0
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- 使用HttpURLConnection发送post和get请求
- HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- HttpURLConnection发送post和get请求
- 使用HttpURLConnection发送post和get请求
- HttpURLConnection发送post和get请求
- 01背包------回溯法(包括回溯法讲解)
- Git branch
- html5基础学习日记0_HTML新建、图片、特殊字符代码、标签
- 代理模式
- 简单聊聊Javascript面向对象(-)
- HttpUrlConnection用get和post请求发送参数
- Spring JDBC详解
- Hibernate之封装utils工具类
- for循环布局button按钮 单选可以取消
- 与DB某学校的联zha考chuan(11.1)(鸽巢+乱搞+x^2的转化)
- log4j.properties 学习
- java-多线程
- js 对象私有属性
- SAP ABAP 与FTP文件交互-进站 文件上传到FTP服务器