用异步获取网络数据Json

先说一下简单的思路,要使用下面是一个小例子  供大家参考 (用异步获取网络数据)(解析Json封装入类放入集合),就需要创建一个类 继承 AsyncTask 在这里说明一下泛型参数

/**
 *    第一个泛型:规定doInBackground方法的参数类型,规定AsyncTask.execute方法传递的参数类型
 *    第二个泛型:规定onProgressUpdate方法参数的类型,publishProgress参数类型
 *    第三个泛型:规定doInBackground方法的返回值类型
 */
public class MyAsyncTask extends AsyncTask<String, Double, String> {
   其中 会有几个重要的方法  在这里简单介绍几个

 
    @Override
    // 运行在主线程,在doInBackground方法之前执行
    protected void onPreExecute() {
        System.out.println("准备联网");
    }
@Override
    // 运行在子线程,做耗时操作
    // 返回值被onPostExecute接收
    protected String doInBackground(String... params) {

        return string;

    }

    // 运行在主线程,在doInBackground方法之后执行,接收doInBackground方法的返回值
    protected void onPostExecute(String result) {

         System.out.println(result);
       

    }

下面是一个小例子  供大家参考 (用异步获取网络数据)


public class MyAsyncTask extends AsyncTask String, String, String> {

private String str;
private ImageView image;

//运行在子线程 做耗时工作,会返回一个字符串
@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    // 请求网络
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(params[0]);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        str = 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 str;//返回一个字符串
}

下面是主Activity  一个方法 (解析Json封装入类放入集合)

// 请求JSON数据
public void ShowJson() {
// 异步加载数据
MyAsyncTask asyncTask = new MyAsyncTask(image);
try {
// 给路径之后必须要给.get()否则获取不了数据
String str = asyncTask.execute(path).get();

//解析Json串 封装入类  加入集合

        Gson gson = new Gson();
        GvRoot gvRoot = gson.fromJson(str, GvRoot.class);
        list = gvRoot.getData();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

先说一下简单的思路,要使用下面是一个小例子  供大家参考 (用异步获取网络数据)(解析Json封装入类放入集合),就需要创建一个类 继承 AsyncTask 在这里说明一下泛型参数

/**
 *    第一个泛型:规定doInBackground方法的参数类型,规定AsyncTask.execute方法传递的参数类型
 *    第二个泛型:规定onProgressUpdate方法参数的类型,publishProgress参数类型
 *    第三个泛型:规定doInBackground方法的返回值类型
 */
public class MyAsyncTask extends AsyncTask<String, Double, String> {
   其中 会有几个重要的方法  在这里简单介绍几个

 
    @Override
    // 运行在主线程,在doInBackground方法之前执行
    protected void onPreExecute() {
        System.out.println("准备联网");
    }
@Override
    // 运行在子线程,做耗时操作
    // 返回值被onPostExecute接收
    protected String doInBackground(String... params) {

        return string;

    }

    // 运行在主线程,在doInBackground方法之后执行,接收doInBackground方法的返回值
    protected void onPostExecute(String result) {

         System.out.println(result);
       

    }

下面是一个小例子  供大家参考 (用异步获取网络数据)


public class MyAsyncTask extends AsyncTask String, String, String> {

private String str;
private ImageView image;

//运行在子线程 做耗时工作,会返回一个字符串
@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    // 请求网络
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(params[0]);
    try {
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        str = 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 str;//返回一个字符串
}

下面是主Activity  一个方法 (解析Json封装入类放入集合)

// 请求JSON数据
public void ShowJson() {
// 异步加载数据
MyAsyncTask asyncTask = new MyAsyncTask(image);
try {
// 给路径之后必须要给.get()否则获取不了数据
String str = asyncTask.execute(path).get();

//解析Json串 封装入类  加入集合

        Gson gson = new Gson();
        GvRoot gvRoot = gson.fromJson(str, GvRoot.class);
        list = gvRoot.getData();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

猜你喜欢

转载自blog.csdn.net/fanaw/article/details/51576888