HttpURLConnection的get请求

首先需要获取HttpURLConnection的实例,需要new出一个URL对象,并传入目标网址,然后调用openConnection()方法即可如下所示:

URL url = new URL("http://www.baidu.com"); 
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

得到HttpURLConnection实例后,一般只需要new出一个URL对象,并且传入目标网络的的地址,然后调用openConnection方法就可以了。然后设置请求方法,连接超时,读取超时以及请求头等

//设置请求参数
mHttpURLConnection.setRequestMethod("GET");
//设置连接超时时间
mHttpURLConnection.setConnectTimeout(15000);
//设置读取超时时间
mHttpURLConnection.setReadTimeout(15000);
//添加请求头
mHttpURLConnection.setRequestProperty("Connection","keep-alive");

然后使用getInputStream方法获取服务器返回的输入流,接着在读取输入流,读取完毕后使用disconnect方法将HTTP连接关闭

/获取服务器返回的输入流
InputStream inputStream = mHttpURLConnection.getInputStream();
//读取输入流s
read = new BufferedReader(new InputStreamReader(inputStream));

完整代码如下:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpURLConnection mHttpURLConnection = null;
                        BufferedReader read = null;
                        try {
                            URL url = new URL("https://www.baidu.com");
                            mHttpURLConnection = (HttpURLConnection) url.openConnection();
                            //设置请求参数
                            mHttpURLConnection.setRequestMethod("GET");
                            //设置连接超时时间s
                            mHttpURLConnection.setConnectTimeout(15000);
                            //设置读取超时时间
                            mHttpURLConnection.setReadTimeout(15000);
                            //添加请求头
                            mHttpURLConnection.setRequestProperty("Connection","keep-alive");
                            //获取服务器返回的输入流
                            InputStream inputStream = mHttpURLConnection.getInputStream();
                            //读取输入流s
                            read = new BufferedReader(new InputStreamReader(inputStream));
                            StringBuilder respose = new StringBuilder();
                            String line;
                            while((line = read.readLine())!=null){
                                respose.append(line);
                            }
                            Log.d("TAG",respose.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }finally {
                            if(read!=null){
                                try {
                                    read.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            if(mHttpURLConnection!=null){
                                //请求结束后关闭HTTP链接
                                mHttpURLConnection.disconnect();
                            }
                        }
                    }
                }).start();
            }
        });
    }
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值