John David
John David

Reputation: 334

Receiving the json data from asynctask to activity

in my activity onCreatemethode im excuting the asynctask as shown below,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new PizzaCustomizeAsyncTask(ActivityPizzaCustomize.this, null).execute();

PizzaCustomizeAsyncTask

@Override
    protected void onPostExecute(JSONArray result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        listener.onTaskCompleted(responseJson);
    }

in this asynctask im getting the response for the responseJson. when i tried to send it back to the onTaskComplete methode it crashes and giving the message application has stopped.

then i have in same activity, which is im using to get the response from the asynctask.

@Override
public void onTaskCompleted(JSONArray responseJson) {

    try {
        List<String> crust = new ArrayList<String>();


        for (int i = 0; i < responseJson.length(); ++i) {
            JSONObject object = responseJson.getJSONObject(i);

            if ((object.getString("MainCategoryID")).equals("1")
                    && (object.getString("SubCategoryID")).equals("1")) {
                Log.i("Crust ", object.getString("Crust"));
                crust.add(object.getString("Crust"));
            }

        }

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

OnTaskCompleted interface

public interface OnTaskCompleted {
    void onTaskCompleted(JSONArray responseJson);
}

problem is occuring when im sending the jsonresponse back to the activity, so is there any problem with this

new PizzaCustomizeAsyncTask(ActivityPizzaCustomize.this, null).execute();

any help will be appreciated to correct this issue.

when i remove the null then it gives following error and the suggestion as shown in the image below. (im getting that suggesstion bz of OnTaskCompleted listener in PizzaCustomizeAsyncTask)

public PizzaCustomizeAsyncTask(Context context, OnTaskCompleted listener) {

        // API = apiURL;
        this.contxt = context;
        this.listener = listener;
    }

enter image description here

Upvotes: 0

Views: 296

Answers (3)

Vamsi
Vamsi

Reputation: 878

You can call PizzaCustomizeAsyncTask like below:

PizzaCustomizeAsyncTask asyncTask =new PizzaCustomizeAsyncTask(new OnTaskCompleted() {

            @Override
            public void onTaskCompleted(Object output) {
                 ....
            }
        });
        asyncTask.execute();


      }

And inside your AsyncTask:

OnTaskCompleted listener;

public PizzaCustomizeAsyncTask(OnTaskCompleted listener) {

        this.listener = listener;
    }

Upvotes: 0

Amrut Bidri
Amrut Bidri

Reputation: 6360

ROOT CAUSE is the interface object is null inside your asynctask;

OnTaskCompleted listener;

change the call to asynctask inside activity oncreate method as :

new PizzaCustomizeAsyncTask(ActivityPizzaCustomize.this, this).execute();

and your constrcutor must be like this:

public PizzaCustomizeAsyncTask(Context context, OnTaskCompleted listener)
{
    this.context=context;
    this.listener=listener;
}

Upvotes: 1

aBrav
aBrav

Reputation: 397

You are not passing a valid listener argument in your constructor.

Instead of

new PizzaCustomizeAsyncTask(ActivityPizzaCustomize.this, null).execute();

you should be passing

new PizzaCustomizeAsyncTask(ActivityPizzaCustomize.this, listener).execute();

assuming you have created a proper listener already.

Upvotes: 0

Related Questions