zoukankan      html  css  js  c++  java
  • Android开发----AsyncTask请求数据

    自建AsyncTask类
    public class MAsyncTask extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(params[0]);
            //请求网络数据
            String string = null;
            
            try {
                HttpResponse response = httpClient.execute(httpPost);
                if(response.getStatusLine().getStatusCode() == 200){
                    
                    HttpEntity entity = response.getEntity();
                    
                    string = EntityUtils.toString(entity,"GBK");
                }
                
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return string;
        }

    }
    mainactivity使用asynctask

    MAsyncTask asyncTask = new MAsyncTask();
            // 得到AsyncTask联网请求到的数据
            try {
                String string = asyncTask.execute(uil).get();
                
                bejson(string);
                
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

  • 相关阅读:
    实时监听输入框值变化的完美方案:oninput & onpropertychange
    展示两行,超出用。。。表示
    修改下拉框滚动条样式
    js单线程工作
    锚点
    火狐图片乱码
    解决重复提交的几种方法
    forward和redirect的区别
    form表单刷新自动提交
    addEventListener和attachEvent的区别
  • 原文地址:https://www.cnblogs.com/bokeyuan007/p/5251575.html
Copyright © 2011-2022 走看看