-3

I'm using a doInBackground method to get a json from a web service so I can use it in my main activity here's what I did but it doesn't work

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {
 @Override
 protected JSONObject doInBackground(JSONObject... data) {
 //.....    
    return json;
 }

 @Override
 protected void onPostExecute(JSONObject jsonObject) {
    super.onPostExecute(jsonObject);

 }
}

and here is my main activity

public void sync(View v){
    LocationsDB mLocationsDB = new LocationsDB(SupportMapActivity.this);
    Cursor events= mLocationsDB.getAllLocations();

    try {
        JSONObject json=makJsonObject(events);
        JSONTransmitter transmitter = new JSONTransmitter();
        AsyncTask<JSONObject, JSONObject, JSONObject> jsonRetour = transmitter.execute(new JSONObject[]{json}); 
//I want to use it here like jsonRetour.getString("something");

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Amine Al
  • 77
  • 2
  • 11
  • AsyncTask is asynchronous. You'll get the result back in onPostExecute(). – BladeCoder Jul 09 '15 at 01:34
  • possible duplicate of [android asynctask sending callbacks to ui](http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui) Either use an Interface to enable a callback into your Activity, or just make the AsyncTask an inner class of the Activity. – Daniel Nugent Jul 09 '15 at 01:38

2 Answers2

0

In the JSONTransmitter, you need to do something with the JSON object in the onPostExecute(). At the moment, your Async task does nothing with it.

Try this:

In the Main activity, have a JSONObject var called mJson and in your onPostExecute do this:

protected void onPostExecute(JSONObject jsonObject) {
  super.onPostExecute(jsonObject);
  mJson = jsonObject;
  // call a method from the MainActivity to do whatever needs to be done with the JSON
}

Of course for this to work, the AsyncTask must be an inner class on the Main Activity.

JDenais
  • 2,956
  • 2
  • 21
  • 30
0

There are multiple ways of doing this but one of my favourite ways is with a callback interface.

Here is the JSONTransmitter:

public class JSONTransmitter extends AsyncTask<JSONObject, JSONObject, JSONObject> {

    private OnJsonTransmitionCompleted mCallback;

    public JSONTransmitter(OnJsonTransmitionCompleted callback) {
        this.mCallback = callback;
    }

    @Override
    protected JSONObject doInBackground(JSONObject... data) {
        //.....    
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        super.onPostExecute(jsonObject);
        this.mCallback.onTransmitionCompleted(jsonObject);
    }

    public interface OnJsonTransmitionCompleted {
        void onTransmitionCompleted(JSONObject jsonObject);
    }
}

The callback interface OnJsonTransmitionCompleted is inside the JSONTrasnmitter as its only use is with the JSONTransmitter.

Here is the main activity:

public class SupportMapActivity extends Activity implements JSONTransmitter.OnJsonTransmitionCompleted{

    public void sync(View v){
        LocationsDB mLocationsDB = new LocationsDB(SupportMapActivity.this);
        Cursor events= mLocationsDB.getAllLocations();

        try {
            JSONObject json = makJsonObject(events);
            JSONTransmitter transmitter = new JSONTransmitter(this);
            transmitter.execute(new JSONObject[]{json}); 

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

    @Override
    public void onTransmitionCompleted(JSONObject jsonObject) {
        // Now you have access to your JSON data in your activity!
    }
}

So we create a callback interface inside the JSONTransmitter which has one method void onTransmitionCompleted(JSONObject jsonObject) that will pass the JSONObject to your activity from onPostExecute(JSONObject jsonObject).

Inside the main activity we implement JSONTransmitter.OnJsonTransmitionCompleted and pass in this when we instantiate JSONTransmitter and lastly we Override void onTransmitionCompleted(JSONObject jsonObject) inside the activity and once our AsyncTask has finished its background process and onPostExecute(JSONObject jsonObject) has been called we will receive the JSONObject inside the implemented method onTransmitionCompleted(JSONObject jsonObject).

I hope this helps here are a few links on this topic:

android asynctask sending callbacks to ui

http://android-er.blogspot.co.il/2013/09/implement-callback-function-with.html

http://blog.redkitmedia.com/android-asynctask-sending-callbacks-to-ui/

http://android-dwivediji.blogspot.co.il/2014/01/best-way-to-use-asynctask-class.html

Community
  • 1
  • 1
Kostyantin2216
  • 616
  • 1
  • 6
  • 15