AsyncTask和JSON,onSuccess不返回任何东西

3

我有一个问题需要询问。我把从AsyncTask函数获取JSONObject(webservice)值的代码贴在这里。我的问题是,我有一个列表并且在onSuccess方法中用json中的数据填充此列表,但稍后在AsyncTask的onPostExecute方法中,“result”为空。

private class getCategories extends AsyncTask<String, Void, List<Category>> {

        private int num_cat_subcat; // category count

        @Override
        protected List<Category> doInBackground(String... arg0) {
            AsyncHttpClient client = new AsyncHttpClient();
            client.get(arg0[0], null, new JsonHttpResponseHandler() {

                public void onSuccess(JSONObject data) {
                    try {
                        JSONObject response = data.getJSONObject("response");
                        JSONArray categories = response
                                .getJSONArray("categories");

                        // elements count
                        num_cat_subcat = categories.length();

                        for (int i = 0; i < categories.length(); i++) {
                            JSONObject item = (JSONObject) categories
                                    .getJSONObject(i);

                            if (item.getString("id").equalsIgnoreCase(
                                    "4d4b7105d754a06376d81259")) {
                                JSONArray subcategories = item
                                        .getJSONArray("categories");

                                // Category --> id, name, pluralName, shortName,
                                // icon_prefix, icon_suffix, primary, parentId
                                Category newItem = new Category(item
                                        .getString("id"), item
                                        .getString("name"), item
                                        .getString("pluralName"), item
                                        .getString("shortName"), null, null,
                                        null);
                                listCategories.add(newItem);
                                newItem = null;

                                if (subcategories.length() > 0) {
                                    // Si tiene subcategorias las contamos.
                                    num_cat_subcat += subcategories.length();

                                    for (int j = 0; j < subcategories.length(); j++) {
                                        JSONObject subitem = (JSONObject) subcategories
                                                .getJSONObject(j);

                                        Category newSubItem = new Category(
                                                subitem.getString("id"),
                                                subitem.getString("name"),
                                                subitem.getString("pluralName"),
                                                subitem.getString("shortName"),
                                                null, null, item
                                                        .getString("id"));
                                        listCategories.add(newSubItem);
                                        newSubItem = null;
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                public void onFailure(Throwable arg0) {

                }
            });
            return listCategories;
        }

        @Override
        protected void onPostExecute(List<Category> result) {
            Log.i("result", result.toString());

            for (int k = 0; k < result.size(); k++) {
                ALLOFTHEM += result.get(k).getId();
                if (k < result.size() - 1) {
                    ALLOFTHEM += ",";
                }
            }

        }

    }

你在 ListView 上看到了想要的数据吗?你的调试监视器是否进入了 onPostExecute 方法? - Nezam
有没有打印任何异常? - Niko Adrianus Yuwono
我认为你应该添加一些日志信息来检查代码的执行点。 - NaserShaikh
你正在使用Loopj API吗? - ρяσѕρєя K
当进入onPostExecute时,它会在logcat中显示一个日志。但是在onSuccess的listCategories内部有数据,在onPostExecute结果外部,它在logcat中显示为[]。 - Víctor Martín
3个回答

3
如果您正在使用Loopj Android Async Http,则无需使用AsyncTask从服务器获取数据并在doInBackground中更新UI,因为onSuccess方法始终在后台计算后在UI线程上执行。只需按如下方式执行即可:
 AsyncHttpClient client = new AsyncHttpClient();
 client.get("Pass Url Here", null, new JsonHttpResponseHandler() {
     @Override
     public void onSuccess(JSONObject data) {
          // update your ListView here
      }
    @Override
    public void onFailure(Throwable arg0, JSONObject arg1) {
        // TODO Auto-generated method stub

         super.onFailure(arg0, arg1);

    }
  });

谢谢,我不知道那个。 - Víctor Martín
我有一个类似的问题!我正在使用AsyncTask使用ρяσѕρєя K方式进行异步登录,但它不起作用,并且我收到了nullPointerException。如果我在主线程中执行登录,则可以正常工作。为什么?我正在使用loopj库! - Pheonix7

2

onPostExecuteddoInBackground执行完毕后被调用。如果AsyncHttpClient的get方法不是阻塞方法,则onPostExecuted会在onSuccess解析结果之前被调用。


为什么这是一个答案而不是评论? - Nezam
4
为什么要写评论? - Blackbelt

0

onPostExecuted在onSuccess解析结果之前被调用,因此尝试在循环中显示一些内容或调试该代码。

    Log.e("Test", item.getString("id"));//inside for Loop

并且在你的for循环之前使用这个

   listCategories = new List<Category>();

希望它有所帮助。

当进入onPostExecute时,它会在logcat中显示一个日志。但是在onSuccess的listCategories内部有数据,在onPostExecute结果外部,它在logcat中显示为[]。 - Víctor Martín

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接