android菜鸟进阶之路—— HttpURLConnection 的实例

在 Android 上发送 HTTP 请求的方式一般有两种,HttpURLConnection 和 HttpClient。下面是 HttpURLConnection 的实例:
       直接贴代码了,在布局文件中是一个按钮和一个TextView,按钮用来发送请求,TextView用来接收服务器返回的消息:
         public class FirstActivity extends Activity implements OnClickListener{
                public static final int SHOW_RESPONSE = 0; //把这个操作事件化,当作一个事件来处理
                private Button send_request;
                private TextView responseText;

                private Handler handler = new Handler(){
                        // 处理返回的Message 
                        public void handleMessage(Message msg) {
                                switch (msg.what) {
                                        case SHOW_RESPONSE:
                                                String response = (String) msg.obj;
                                                //  在这里进行UI操作,将结果显示到界面上
                                                responseText.setText(response);
                                }
                          }
                   };

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_first);
                        send_request = (Button) findViewById(R.id.btn_send_request);
                        responseText = (TextView) findViewById(R.id.tv_response);
                        send_request.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(v.getId()==R.id.btn_send_request){
                                sendRequestWithHttpURLConnection();
                        }
                }

                private void sendRequestWithHttpURLConnection() {
                        // 开启线程来发起网络请求
                    new Thread(new Runnable(){
                        @Override
                        public void run() {
                                // TODO Auto-generated method stub
                                HttpURLConnection connection = null;
                                try {
                                        // 1,获取URL和 HttpURLConnection 对象
                                        URL url = new URL(" http://www.baidu.com");
                                        connection = (HttpURLConnection) url.openConnection();
                                        // 2,确定请求方法
                                        connection.setRequestMethod("GET");
                                         // 3,确定连接的各种属性
                                        connection.setConnectTimeout(8000);
                                        connection.setReadTimeout(8000);
                                        // 4,得到和读取连接得到的输入流,并进行处理
                                        InputStream in = connection.getInputStream();
                                        // 下面对获取到的输入流进行读取
                                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                                        StringBuilder response = new StringBuilder();
                                        String line;
                                        // 逐行读取赋给line,然后再添加给response字符串
                                        while((line = reader.readLine())!=null){
                                                response.append(line);
                                        }

                                        Message message = new Message();
                                        message.what = SHOW_RESPONSE;
                                        //  将服务器返回的结果存放到Message中
                                        message.obj = response.toString();
                                        handler.sendMessage(message);
                                    } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                    }finally{
                                            if (connection != null) {
                                                    connection.disconnect();
                                            }
                                    }
                            }
                        }).start();
              }
    }
     具体的代码解释已不再赘述,关键的流程还是很简单的,共同进步。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值