得到的AsyncTask返回的JSON [英] Get returned JSON from AsyncTask

查看:109
本文介绍了得到的AsyncTask返回的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我必须延伸AsyncTask的这部分代码类。然后,我做的新型装载机()执行(); ,但我想使用 JSONArray响应这我Loader类的返回我该怎么办呢?因为我需要它在几个不同的地方?或者我应该只是提出我的code到onPostExecute,并从那里做的一切?

 公共类加载器扩展的AsyncTask<字符串,整数,JSONArray> {

    ProgressDialog对话框;

    在preExecute保护无效(){

        对话框= ProgressDialog.show(ChallengeList.this,,Laddar ...);
        dialog.setCancelable(真正的);
    }

    @覆盖
    保护JSONArray doInBackground(字符串... PARAMS){


    JSONArray响应=无效;
    HttpClient的客户端=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(listURL);

    尝试 {

        HTT presponse RESP = client.execute(httppost);
        状态行状态行= resp.getStatusLine();
        INT状态code = statusLine.getStatus code();
        Log.i(状态code,状态code+状态code);
        如果(状态code == 200){
            最后的JSONObject JSON =新的JSONObject();

            json.put(用户ID,prefs.id());

            响应= SendHttp.parseHttp(listURL,JSON);

        }
    }赶上(JSONException E1){
        e1.printStackTrace();
    }赶上(IOException异常E1){
        e1.printStackTrace();
    }

        返回响应;
    }
    保护无效onPostExecute(JSONArray结果){
        dialog.dismiss();
    }
}
 

解决方案

方法 onPostExecute 有作为参数 JSONArray 你的 doInBackground 方法返回。

onPostExecute 运行在你的主(来电)活动的线程,因此除了关闭对话框中的方法,您可以处理结果阵列进一步,安全地将它传递给其他的方法,等等:

  @覆盖
保护无效onPostExecute(JSONArray结果)
{
    super.onPostExecute(结果);
    最后的消息味精=新的Message();
    msg.obj =结果;
    如果(youWantToUseHandler)
        handler.dispatchMessage(MSG);
    其他
        writeJSONArray(结果);
}
 

处理

 最后的处理程序处理程序=新的处理程序()
{
    公共无效的handleMessage(信息MSG)
    {
        最终JSONArray结果=(JSONArray),其中msg.obj;
        writeJSONArray(结果);
    };
};
 

其他方法:

 私人无效writeJSONArray(最终JSONArray结果)
{
    的for(int i = 0; I< result.length();我++)
    {
        尝试
        {
            Log.d(样品,result.get(ⅰ)的ToString());
        }
        赶上(JSONException E)
        {
            Log.e(SAMPLE,错误得到结果+ I,E);
        }
    }
}
 

由于 onPostExecute 运行doInBackground后的UI线程。指定的结果是由doInBackground返回的值或空,如果任务被取消或异常发生。 〜API文档 你可以叫你在你的类已经声明的任何方法,并通过这个数组作为参数传递给它。

So I have this loader class which extends AsyncTask. Then I do new loader().execute(); but I want to use the JSONArray response which my loader class returns how do I do that? Because I need it in several different places? Or should I just move my code to onPostExecute and do everything from there?

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

解决方案

The method onPostExecute has as parameter the JSONArray you returned from the doInBackground method.

onPostExecute runs on your main (caller) activity's thread, so besides dismissing the dialog in that method, you can process the result array further, pass it safely to other methods, etc:

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

the handler:

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

Some other method:

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

Since onPostExecute "Runs on the UI thread after doInBackground. The specified result is the value returned by doInBackground or null if the task was cancelled or an exception occured." ~API Docs You can call any method you've declared in your class, and pass this array as a parameter to it.

这篇关于得到的AsyncTask返回的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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