0

I try to use a string from onPostExecute method but I have to code an interface or a handler for this process. I read something but it's not clear for me. My onPostExecute method:

protected void onPostExecute(String result) {
            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);

                t = json_data.getString("name");


                 names.add(t);
              }


        } catch (JSONException e1) {
            e1.printStackTrace();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        super.onPostExecute(result);
    }`
newuser
  • 8,338
  • 2
  • 25
  • 33
elfoz
  • 115
  • 1
  • 8
  • 17

1 Answers1

3

Async functions cannot return values because they are not called like normal functions. instead of being triggered by another function, they're triggered by an event handler.

In this case, you need to define a promise (using a deferred object, https://github.com/CodeAndMagic/android-deferred-object) which basically tells the function you want to return to "I don't yet know what it will be, but I will tell you when I got it."

Nzall
  • 3,439
  • 5
  • 29
  • 59
  • I think, I have to use this solution: http://stackoverflow.com/questions/13815807/return-value-from-asynctask-class-onpostexecute-method but I can't understand that in void onTaskCompleted(values) what are refer "values" ? – elfoz Oct 02 '13 at 09:58