zoukankan      html  css  js  c++  java
  • httpclient + AsyncTask 请求数据 || httpclient + handler 请求数据

    public class MyAsy extends AsyncTask<String, Integer, String> {

        private String json;

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            // 实例化HttpClient
            HttpClient client = new DefaultHttpClient();
            // 设置请求方式
            HttpGet httpGet = new HttpGet(params[0]);
            try {
                // 请求数据
                HttpResponse execute = client.execute(httpGet);
                // 通过状态行得到状态码
                int statusCode = execute.getStatusLine().getStatusCode();
                // 判断状态码是否是200 是 请求成功
                if (statusCode == 200) {
                    HttpEntity entity = execute.getEntity();
                    // 拿到请求的数据
                    json = EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return json;
        }
    }

    // 实例化AsyncTask
            MyAsy myAsy = new MyAsy();
            try {
                // 异步交互拿到数据
                String string = myAsy.execute("  ").get();
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Handler handler = new Handler() {
                public void handleMessage(android.os.Message msg) {

                    if (msg.what == 1) {
                        String str = (String) msg.obj;

                    }

                };
            };

            new Thread() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    super.run();

                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet("添加网址");
                    try {
                        HttpResponse execute = client.execute(httpGet);

                        int statusCode = execute.getStatusLine().getStatusCode();
                        if (statusCode == 200) {
                            HttpEntity entity = execute.getEntity();
                            String json = EntityUtils.toString(entity);

                            Message msg = Message.obtain();
                            msg.what = 1;
                            msg.obj = json;
                            handler.sendMessage(msg);
                        }

                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
           

  • 相关阅读:
    23种设计模式-----行为模式
    23种设计模式-----创建型模式、结构型模式
    字节码操作、javassist使用
    反射机制(reflection)
    NFC手机
    NFC简介
    不同技术的过滤条件的定义
    [linux] ubuntu系统tips
    图算法(一)
    跳跃表skiplist
  • 原文地址:https://www.cnblogs.com/cuizhe/p/5405615.html
Copyright © 2011-2022 走看看