3

I'm here with another question. I post you the code from my AsyncTask function to get values from a JSONObject (webservice). My problem is that I have a List and I fill this list with the data from the json in the onSuccess method, but later, in the onPostExecute method of the AsyncTask, "result" is null.

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 += ",";
                }
            }

        }

    }
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94

3 Answers3

3

if you are using loopj android-async-http then no need to use AsyncTask for getting data from server doInBackground and updating UI in onPostExecute because onSuccess method always execute on UI Thread after background computation. just do it without AsyncTask as :

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

    }
  });
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thank you, I don't know that – Víctor Martín May 28 '13 at 20:24
  • i've got a similar problem! im doing a async login through AsyncTask using ρяσѕρєя K way, but its not working and im getting a nullPointerException. if i do the login in the main thread it works fine. why? i am using loopj library! – Pheonix7 Nov 29 '13 at 15:58
2

onPostExecuted is called when the doInBackground runs out its execution. If the get method of AsyncHttpClient is not a blocking method, onPostExecuted is called before onSuccess parses the result

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

onPostExecuted is called before onSuccess parses the result, So try display something in your loop or debug that code.

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

And Also use this before your for loop

   listCategories = new List<Category>();

Hopes it help.

Zeeshan Chaudhry
  • 842
  • 2
  • 15
  • 31
  • It shows me a log in logcat when enter the onPostExecute. But inside the onSuccess listCategories have the data, and outside, on onPostExecute result it is shows as [] in the logcat – Víctor Martín May 28 '13 at 20:14