如何从 AsyncTask 的类中获取 JsonArray [英] How to get JsonArray from a class who AsyncTask

查看:33
本文介绍了如何从 AsyncTask 的类中获取 JsonArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类来从 url 检索 JsonArray.此类扩展 AsyncTask 以避免在接口上创建多个 AsyncTask.

I'm trying to create a class to retrieve a JsonArray from a url. This class extends AsyncTask to avoid creating multiple AsyncTasks on interfaces.

该类在调试器上运行良好,但我不知道如何获取返回对象.有人可以帮我吗?

The class works fine on debugger, but I do not know what to do to get the return object. Can anyone help me?

这是我的课程:

public class QueryJsonArray extends AsyncTask<String, Void, Void>{

JSONArray jsonRetorno = null;

@Override
protected Void doInBackground(String... params) {

    InputStream is = null;
    String result = "";

    try {           

        Log.i(getClass().getName(), params[0]);
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet( params[0]);
        HttpResponse response = httpclient.execute(httpGet);
        if(response.getStatusLine().getStatusCode()==200){
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }else{

            is = null;
        }


    } catch(Exception e) {

        e.printStackTrace();

    }

    if(is!=null){
        try {           
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "
");
            }
            is.close();
            result = sb.toString();             
        } catch(Exception e) {
            e.printStackTrace();
        }

        try {
            jsonRetorno = new JSONArray(result);            
        } catch(JSONException e) {
            e.printStackTrace();

        }

    }
    return null;
      }
         }

我想这样检索:

QueryJsonArray obj = new QueryJsonArray();
JSONArray jArray = obj.execute(myUrl);

提前致谢.

推荐答案

您需要使用回调.只需添加以下内容...

You need to use a callback. Simply add the following…

变量:

private Callback callback;

内部接口:

public interface Callback{
    public void call(JSONArray array);
}

构造函数:

public QueryJsonArray(Callback callback) {
    this.callback = callback;
}

此外,将您的类声明更改为:

Additionally, change your class declaration to:

public class QueryJsonArray extends AsyncTask<Void, Void, JSONArray>

并将doInBackground的返回类型改为JSONArray.

doInBackground的末尾,添加:

return jsonRetorno;

最后,在内容中添加以下方法:

Finally, add the following method with contents:

public void onPostExecute(JSONArray array) {
    callback.call(array);
}

<小时>

现在,要执行任务,只需:


Now, to execute the task, just do:

QueryJsonArray obj = new QueryJsonArray(new Callback() {
    public void call(JSONArray array) {
        //TODO: here you can handle the array
    }
});
JSONArray jArray = obj.execute(myUrl);

<小时>

附带说明,您可以使用第三方库(例如 droidQuery)极大地简化所有这些操作,这会将上述所有代码压缩为:


As a side note, you could greatly simplify all of this using a third party library, such as droidQuery, which would condense all of the above code to:

$.ajax(new AjaxOptions().url(myUrl).success(new Function() {
    public void invoke($ d, Object… args) {
        JSONArray array = (JSONArray) args[0];
        //TODO handle the json array.
    }
});

这篇关于如何从 AsyncTask 的类中获取 JsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆