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();
            }
        }

  • 相关阅读:
    前后端分离的思想
    原生js瀑布流
    瀑布流懒加载
    js的垃圾回收机制
    TCP三次挥手四次握手
    HTTP与HTTPS的区别
    http报文
    前后端的分离
    express中间件
    vue生命周期钩子函数解读步骤
  • 原文地址:https://www.cnblogs.com/bokeyuan007/p/5251575.html
Copyright © 2011-2022 走看看