3

所以我有这个扩展 AsyncTask 的加载器类。然后我做new loader().execute();了,但我想使用我的加载器类返回的JSONArray 响应,我该怎么做?因为我在几个不同的地方都需要它?还是我应该将我的代码移到 onPostExecute 并从那里做所有事情?

public class loader extends AsyncTask<String, Integer, JSONArray> {

    ProgressDialog dialog;

    protected void onPreExecute() {

        dialog = ProgressDialog.show(ChallengeList.this, "", "Laddar...");
        dialog.setCancelable(true);
    }

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


    JSONArray response = null;
    HttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(listURL);

    try {

        HttpResponse resp = client.execute(httppost);
        StatusLine statusLine = resp.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        Log.i("Statuscode", "statusCode"+statusCode);
        if (statusCode == 200) {
            final JSONObject json = new JSONObject();

            json.put("userID", prefs.id());

            response = SendHttp.parseHttp(listURL, json);

        }
    } catch (JSONException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    } 

        return response;
    }
    protected void onPostExecute(JSONArray result) {
        dialog.dismiss();
    }
}
4

2 回答 2

4

该方法onPostExecute具有JSONArray您从该doInBackground方法返回的参数。

onPostExecute在您的主(调用者)活动的线程上运行,因此除了关闭该方法中的对话框之外,您还可以result进一步处理数组,将其安全地传递给其他方法等:

@Override
protected void onPostExecute(JSONArray result)
{
    super.onPostExecute(result);
    final Message msg = new Message();
    msg.obj = result;
    if (youWantToUseHandler)
        handler.dispatchMessage(msg);
    else
        writeJSONArray(result);
}

handler:_

final Handler handler = new Handler()
{
    public void handleMessage(Message msg) 
    {
        final JSONArray result = (JSONArray)msg.obj;
        writeJSONArray(result);
    };
};

其他一些方法:

private void writeJSONArray(final JSONArray result)
{
    for (int i = 0; i < result.length(); i++)
    {
        try
        {
            Log.d("SAMPLE", result.get(i).toString());
        }
        catch (JSONException e)
        {
            Log.e("SAMPLE", "error getting result " + i, e);
        }
    }
}

由于onPostExecute“在doInBackground之后在UI线程上运行。指定的结果是doInBackground返回的值,如果任务被取消或发生异常,则为null。” ~API Docs 你可以调用你在类中声明的任何方法,并将这个数组作为参数传递给它。

于 2012-05-13T20:48:52.077 回答
0

下载内容网页后,您可以在 onPostExecute 方法中使用以下代码:

protected void onPostExecute(String s) { super.onPostExecute(s);

        try {
            JSONObject jsonObject = new JSONObject(s);

            String weatherInfo = jsonObject.getString("weather");

            Log.i("Weather content", weatherInfo);

            JSONArray arr = new JSONArray(weatherInfo);

            for (int i=0; i < arr.length(); i++) {
                JSONObject jsonPart = arr.getJSONObject(i);

                Log.i("main",jsonPart.getString("main"));
                Log.i("description",jsonPart.getString("description"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
于 2018-05-22T04:09:44.667 回答