如何从谁的AsyncTask类获得JsonArray谁的、AsyncTask、JsonArray

2023-09-07 01:26:13 作者:乱了思绪。

我想创建一个类来从URL检索JsonArray。 此类扩展的AsyncTask,以避免在接口上创建多个AsyncTasks。

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 + "\n");
            }
            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);

在此先感谢。

Thanks in advance.

推荐答案

您需要使用的回调的。只需添加以下...

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 ,这将凝聚所有上述code到:

使用VS Code编写 调试和运行C 程序 vs代码写好了怎么运行 TT努力填坑的博客 CSDN博客

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.
    }
});