AsyncTaskDemo

//创建的类
//使用asynctask联网,
//urlString解析的地址
new MAsyncTask().execute(urlString);

/**
* 异步任务类
* 第一个泛型:execute方法传递过来的参数类型
* 第二个泛型:是中间过程更新进度用的一个泛型;
* 第三个泛型:async返回的数据类型,我们这里返回的是json;
*/
class MAsyncTask extends AsyncTask{

    //这个方法是子线程方法;在这里进行联网,把json数据传给主线程(onPostExecute)更新UI;
    @Override
    protected String doInBackground(String... strings) {//可变参数是个数组
        String netJson = NetUtil.getNetJson(strings[0]);
        return netJson;
    }

    //主线程更新拿到json,更新ui
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        ProductBean productBean = gson.fromJson(s, ProductBean.class);
        //一定要转换集合,使用addAll方法;
        dataBeanList.addAll(productBean.getData());
        mAdapter.notifyDataSetChanged();//因为这是主线程,可以更新ui;


    }
}

你可能感兴趣的:(AsyncTaskDemo)