第一行代码总结:10网络:10.2使用HTTP协议访问网络10.2.1 使用HttpURLConnection10.2使用HTTP协议访问网络 工作原理: 客户端向服务器发出一条HTTP请求,服务器收

本文详细介绍了在Android上使用HttpURLConnection发送HTTP请求的过程,包括获取URL对象、创建HttpURLConnection对象、设置请求方法、定制连接参数、读取服务器响应以及处理网络权限等内容。

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

10.2使用HTTP协议访问网络

工作原理:

客户端向服务器发出一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理就可以了。

在Android上发送HTTP请求的方式一般有两种:

HttpURLConnection

HttpClient

 

10.2.1 使用HttpURLConnection

使用步骤:

1、获取URL对象,传入网络地址;

   URL url = newURL("http://www.baidu.com");

2、获取HttpURLConnection对象。

   HttpURLConnection connection = (HttpURLConnection)url.openConnection();

3、设置HTTP请求所使用的方法。常用的方法有两个:GET/POST.

connection.setRequestMethod("GET");

4、进行一些自由的定制

connection.setConnectTimeout(8000);  //设置连接超时时间

connection.setReadTimeout(8000); //设置读取超时时间

5、调用getInputStream()方法,获取到服务器返回的输入流:

InputStream in = connection.getInputStream();

6、对输入流进行读取:

//下面对获取到的输入流进行读取

               BufferedReader reader = new BufferedReader(new InputStreamReader(in));

               StringBuilder response = new StringBuilder();

               String line;

               //从高效缓冲输入流当中读取字符串并拼接

               while((line = reader.readLine()) != null){

                  response.append(line);

               }

7、最后调用disconnect()方法,将之歌HTTP连接关闭掉;

finally{

               if(connection != null){

                  connection.disconnect();

               }

            }

8、声明网络权限:

    uses-permissionandroid:name=”android.permission.INTERNET”

 

代码示例:

private void sendRequestWithURLConnection() {

      // 开启线程来发起网络请求

      new Thread(new Runnable(){

 

          @Override

          public void run() {

             HttpURLConnection connection = null;

             try {

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

                connection = (HttpURLConnection)url.openConnection();

                connection.setRequestMethod("GET");

                connection.setConnectTimeout(8000);

                connection.setReadTimeout(8000);

                InputStream in =connection.getInputStream();

                //下面对获取到的输入流进行读取

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                StringBuilder response = new StringBuilder();

                String line;

                //从高效缓冲输入流当中读取字符串并拼接

                while((line = reader.readLine()) != null){

                   response.append(line);

                }

                //将从网络当中读取到的内容返回给主线程:利用异步消息处理机制,handler

                Message message = new Message();

                message.what = SHOW_RESPONSE;

                //将服务器返回的结果存放到Message

                message.obj = response.toString();

                handler.sendMessage(message);

               

             } catch (Exception e) {

                e.printStackTrace();

             }finally{

                if(connection != null){

                   connection.disconnect();

                }

             }

          }

         

      }).start();

     

   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值