1

I am calling soap webservice and need to display what is returned. but I couldnt do it because AsyncTask is complex and I dont know how to use it properly. would you please tell me how to return data from the called function via asynctask?

here is my code

public class WebserviceTool {

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://192.168.0.11:9289/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/get_currency";
private final String METHOD_NAME = "get_currency";

public static void main(String[] args) {
    // TODO Auto-generated method stub
}

public String execute_barcode_webservice(String s1, String s2) {
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("date",s1);
    request.addProperty("cur_code",s2);
    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.encodingStyle = SoapEnvelope.ENC;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            Object response;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        response = (Object) envelope.getResponse();
        Log.i("my_error", response.toString());
    } catch (Exception e) {
        Log.i("my_error", e.getMessage().toString());
    }
    return "testarif";
}

public class AsyncCallWS extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... params) {
        try {
            execute_barcode_webservice(params[0], params[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }   
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }
}
}

this is the function execute_barcode_webservice() that does all the job and returns data. but since I call execute_barcode_webservice() view AsyncTask, I dont know how to return with it. how can I do it?

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

1 Answers1

5

The result of the async task execution is the response object produced by execute_barcode_webservice(). However, don't think about the async task as something that will execute and return a value to you. Instead, inside the method onPostExecute() you must take the response object and process it (extract its values and display them in a list, or whatever you want to do with it). The async task is just a way to execute some code in a separate thread then go back to the main thread (the UI thread) and process the results, which is done in onPostExecute().

My suggestion: rewrite execute_barcode_webservice() to return a response object instead of a String (an object that can be null if the operation fails) and pass that object to the onPostExecute() method. You will have to change the async task to:

public class AsyncCallWS extends AsyncTask<String, Void, Object> {
    @Override
    protected Object doInBackground(String... params) {
        Object response = null;
        try {
            response = execute_barcode_webservice(params[0], params[1]);
        } catch (Exception e) {
            // TODO: handle exception
        }   
        return response;
    }

    @Override
    protected void onPostExecute(Object response) {
        if (response != null) {
            // display results in a list or something else
        }
    }
Piovezan
  • 3,215
  • 1
  • 28
  • 45
  • in doinbackground function, you are returning and object but return type is void – Arif YILMAZ Jun 14 '13 at 22:50
  • @ayilmaz: Thanks, I have fixed the code. See, you already grasped the concept! – Piovezan Jun 14 '13 at 22:51
  • how do you display returned data in onPostExecute() ? I want to return the data where asynctask is called – Arif YILMAZ Jun 14 '13 at 22:53
  • So, why do you need to use AsyncTask in this case? – bancer Jun 14 '13 at 22:56
  • "I want to return the data where asynctask is called" - This is not a good idea because you will have to wait the async task execute before you can have the data (with a `while()` for example), and this violates the point of parallel execution. Whatever you intend to do with the resulting data, do it in `onPostExecute()` or still in `doInBackground()`. If you want to display the data, it has to be done in `onPostExecute()`. – Piovezan Jun 14 '13 at 22:56
  • how do u set the textbox's text in onpostexecute? how do u get the reference to textbox from activity? – Arif YILMAZ Jun 14 '13 at 23:40
  • @ayilmaz: Create a constructor for `AsyncCallWS` and pass the textbox reference to the constructor. – Piovezan Jun 14 '13 at 23:56