HttpUrlConnection用get和post请求发送参数
来源:互联网 发布:网络电视如何看直播 编辑:程序博客网 时间:2024/06/08 13:57
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
- 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(); }; });
阅读全文
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请求
- 矩阵快速幂
- Meta标签与搜索引擎优化
- 你知道 Android 的 MessageQueue.IdleHandler 吗?
- 171103 深度学习主机配置暑假攒机小记
- AJAX如何向服务器发送请求
- HttpUrlConnection用get和post请求发送参数
- [EMNLP2017]Adversarial Training for Relation Extraction
- Django与JS通信基本方法及原理;Django与jQuery通信;Django前后端传值
- JS制作计时器(毫秒级),简单、易懂
- day11
- androidd studio 打包签名 V1&V2
- EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式
- Android Debug.startMethodTracing() 报错 Unable to open trace file '/sdcard/*.trace': Permission denied
- 字符串比较