0

In my application I need to call a web service to get some data from the internet. To avoid blocking the UI I use an AsyncTask to do that. But I'm having problems to pass the return value correctly for further use. I guess I'm making some architectural mistakes here. So this is how my application is/should be structured:

MainActivity: An Activity containing a TextView, a Button and a SoapParser (see below). I want to call the web service by pressing the Button, parse the result and display it in the TextView.

SoapRequestTask: An AsyncTask with a callback mechanism like this. The return value of doInBackground() is a SoapObject and is passed to the listener as a parameter in the callback method listener.onTaskCompleted(SoapObject) in onPostExecute().

SoapRequestManager: A class that should have a method public SoapObject makeRequest(someParams). This method is supposed to use my AsyncTask and return my SoapObject from its doInBackground().

MySpecialRequestManager: Extends SoapRequestManager and has different methods to call the web service for different results. Each of these methods uses makeRequest(someParams) of the superclass.

SoapParser: A class that parses a SoapObject to a String so it can be displayed in my Activity's TextView. It should have a method like public String parseResult(SoapObject).

How do I use the callback and the return value correctly? Which class should implement the callback interface?

Community
  • 1
  • 1
kaolick
  • 4,817
  • 5
  • 42
  • 53
  • Have SoapRequestManager implement BroadcastReceiver and send a broadcast that for it to receive that contains a bundle with the data in question. – zgc7009 May 13 '14 at 17:21
  • @zgc7009 Could you please give me a code example? – kaolick May 13 '14 at 18:23

1 Answers1

0

Just a quick snippet because you asked for some code. This might help get you started

In the AsyncTask's postExecute() method in SoapRequestTask

Intent intent = new Intent("send_data");
intent.setAction("SEND_DATA");              // don't NEED this, as we implicitly
                                            // declare our intent during initialization
intent.putExtra("data_return_key", mData);  // where mData is what you want to return
sendBroadcast(intent);

Then for SoapRequestManager

public class SoapRequestManager implements BroadcastReceiver{
    int mData;                        // assuming our data is an int in this example

    // Default constructor
    public SoapRequestManager(){
        registerReceiver(this, new IntentFilter("send_data"));
        // if you use setAction use the below registration instead
        // registerReceiver(this, new IntentFilter("SEND_DATA"));
    }

    //... All of the stuff you have in the class already

    @Override
    public void onReceive(Context context, Intent intent){
        // For this example let's say mData is an integer value

        // if you use setAction, check that the intent is the correct one via
        // if (intent.getAction().equals("SEND_DATA"))
        mData = intent.getIntExtra("data_return_key");
    }
}
zgc7009
  • 3,371
  • 5
  • 22
  • 34