asynctask的使用

来源:互联网 发布:淘宝蚂蚁摄影怎么样 编辑:程序博客网 时间:2024/06/05 04:46
public class MainActivity extends AppCompatActivity {    private ListView listView;    private ProgressDialog progressDialog;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找到控件        listView = (ListView) findViewById(R.id.listview);        try {            loadNetFromGet("http://api.expoon.com/AppNews/getNewsList/type/1/p/");        } catch (Exception e) {            e.printStackTrace();        }    }    /**     *     * 使用AsyncTask结合HttpURLConnection请求数据     */    public void loadNetFromGet(String path){        new AsyncTask<String,Void,String>(){            @Override            protected void onPreExecute() {                //调用进度条的方法                pressDialog();            }            @Override            protected void onPostExecute(String s) {                progressDialog.dismiss();                if(s==null){                    return;                }                System.out.println(s);                //1.解析JSON数据(系统自带的api,google 发布的gson.jar )                Gson gson = new Gson();                Big big = gson.fromJson(s, Big.class);                //得到集合                List<Big.DataBean> data = big.getData();                //绑定到适配器                MyAdapter myAdapter = new MyAdapter(MainActivity.this,data);                //将数据映射到ListView展示                listView.setAdapter(myAdapter);            }            @Override            protected String doInBackground(String... params) {                try {                    String path = params[0];                    URL url = new URL(path);                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();                    connection.setRequestMethod("GET");                    connection.setConnectTimeout(5000);                    connection.setReadTimeout(5000);                    int code = connection.getResponseCode();                    if(code == 200){                        InputStream is = connection.getInputStream();                        String result = readStream(is);                        //请求太快睡眠两秒                        Thread.sleep(2000);                        return result;                    }                } catch (Exception e) {                    e.printStackTrace();                }                return null;            }        }.execute(path);    }    //转换字符流    public String readStream(InputStream is){        try {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            byte[] b = new byte[1024];            int len;            while((len = is.read(b))> 0 ){                baos.write(b,0,len);            }            return baos.toString();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    //进度条    public void pressDialog(){        progressDialog = new ProgressDialog(MainActivity.this);        progressDialog.setMessage("玩命加载中...");        progressDialog.show();    }}
原创粉丝点击