asynctask的使用

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值