HttpUrlConnection用get和post请求发送参数

本文详细介绍了如何使用HttpURLConnection发送GET和POST请求,包括设置连接超时时间、读取超时时间、组装请求参数、发送请求、处理响应等过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 向服务器发送post和get请求 并接收响应

HttpURLConnection 向服务器发送post和get请求 并接收响应

月薪30k的前端程序员都避开了哪些坑?

程序员薪水有高有低,同样工作5年的程序员,有的人月薪30K、50K,有的人可能只有5K、8K。是什么因素导致了这种差异?

使用HttpURLConnection向服务器发送post和get请求

HttpConnection的请求方式主要有两种,get和post,post请求和get请求的最大不同就是提交请求信息的方式,post是通过把请求信息封装在http请求头中发送出去的,get请求是把请...

使用HttpURLConnection向服务器发送post和get请求

一、使用HttpURLConnection向服务器发送get请求1、向服务器发送get请求 @Test publicvoid sendSms() throws Excep...

使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

Http学习之使用HttpURLConnection发送post和get请求 .

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

Http学习之使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

使用HttpURLConnection发送get和post请求

转载请注明出处:我们在开发的使用,直接使用的开源框架,例如:Xutil,Volley开源框架直接访问网络,但是我们也需要知道其中的一些知识,了解一下怎样访问网络的。下面我们模拟以下客户端和服务端,看...

Http学习之使用HttpURLConnection发送post和get请求

Http学习之使用HttpURLConnection发送post和get请求最常用的Http请求无非是get 和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给serv...

Http学习之使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

Android开发点点滴滴——使用HttpURLConnection发送get和post请求

android和PC通信,http方式是必不可少的,如何使用android来实现发送http请求,并且得到http的回复消息呢?可以使用HttpURLConnection,来实现:1.发送get请...

HttpURLConnection发送post和get请求

http://blog.youkuaiyun.com/chenlei1889/article/details/6363356 最常用的Http请求无非是get和post,get请求可以获取静态页面,也...

Http学习之使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

Http学习之使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

Http学习之使用HttpURLConnection发送post和get请求

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...

使用HttpURLConnection发送post和get请求

使用HttpURLConnection发送post和get请求1、http://blog.youkuaiyun.com/pandazxx/archive/2007/06/18/1657109.aspx2、http...

Android例子—HttpURLConnection发送POST、GET请求代码示例

这里我们主要针对GET和POST请求写两个不同的使用示例,我们可以conn.getInputStream() 获取到的是一个流,所以我们需要写一个类将流转化为二进制数组!工具类如下:StreamToo...

Android 使用HttpURLConnection发送Post/Get请求

HTTP规范定义中最常用的请求类型就是Get和Post。当你在浏览器里输入任意一个网址按回车,浏览器即已经在执行Get请求了;当你回复了某条微博时,这时可能就执行了一次Post请求。简单的来说,Get...

Http学习之使用HttpURLConnection发送post和get请求

转自  http://blog.youkuaiyun.com/pandazxx/article/details/1657109最常用的Http请求无非是get和post,get请求可以获取静态页面...

HttpURLConnection发送Get和Post请求

HttpURLConnection是java的标准类,可发送get请求和post请求。关于Get和Post的区别,这里就不细说了,网上普遍的说法是:1、GET请求是从服务器上获取数据,POST请求...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值