Search code examples
javaandroidweb-servicesandroid-asynctaskandroid-ksoap2

how to use asynctask properly with ksoap2 and axis2 web service


I developed a web service from a dynamic web project using the web service wizard(followed a tutorial so I wasn't really sure what I was doing). The tutorial made me install the axis2 plugin so I'm assuming that's the one used for the web service generation. Anyhow, the service is working when I tried it from my browser so no problem in that part.

My problem is, I want to access this web service from an android application. I read a lot of tutorials past two days and tried to implement one, but later realized that it was outdated and it tried to connect to network in the main thread, which is not allowed after android 3.0. Then I found out about asynctask and tried my hand in it but there is just so much that I don't understand. The tutorials I found didn't explain any of those so I'm asking here.

The code I wrote before hearing about the asynctask is here:

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private final String NAMESPACE = "http://eko.erdem.com";
private final String URL = "http://159.20.89.38:9398/EkoBiletTest/services/EkoClass?wsdl";
private final String SOAP_ACTION = "http://eko.erdem.com/homePage";
private final String METHOD_NAME = "homePage";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    String place = "Paris";
    String checkinDate = "2013-06-10";
    String checkoutDate = "2013-06-12";

  //Pass value for place variable of the web service
    PropertyInfo placeProp =new PropertyInfo();
    placeProp.setName("place");//Define the variable name in the web service method
    placeProp.setValue(place);//Define value for place variable
    placeProp.setType(String.class);//Define the type of the variable
    request.addProperty(placeProp);//Pass properties to the variable

  //Pass value for checkinDate variable of the web service
    PropertyInfo checkinDateProp =new PropertyInfo();
    checkinDateProp.setName("checkinDate");
    checkinDateProp.setValue(checkinDate);
    checkinDateProp.setType(String.class);
    request.addProperty(checkinDateProp);


  //Pass value for checkinDate variable of the web service
    PropertyInfo checkoutDateProp =new PropertyInfo();
    checkoutDateProp.setName("checkoutDate");
    checkoutDateProp.setValue(checkoutDate);
    checkoutDateProp.setType(String.class);
    request.addProperty(checkoutDateProp);



    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();


        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setText(response.toString());
        setContentView(tv);

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Now I don't know which parts of the code should be in which function of the asynctask ( doinBackground() etc.) Also I don't understand the parameter sending system of asynctask. For example if I wanted to pass the NAMESPACE, URL, METHOD_NAME etc. to the asynctask how should I do it? Also where should I update the textView of the main activity? If someone can point me to a tutorial that covers these questions it would be great. I would also appreciate any answer of your own to those questions. Thanks in advance.


Solution

  • Any network related work needs to be done in the doInBackground method of the AsyncTask

    to send data to the asynctask you need to declare the incoming data for the class

    puclic class MyNetowrkTask extends AsyncTask<String[],Void,Void>
    

    the first parameter in the <> is the imcoming data, the second is the progress and the thirs is the result you return to the onPostExecute when the task is done.

    so to pass strings in you would do something like this

    new MyNetworkTask().execute(new String[] {"namespace","url","method"});
    

    the in the doInBackground you get the data like this

    protected Void doInBackground(String... params) {
        String[] data = params[0];
        String namespace = data[0];
        String url = data[1];
        String method = data[2];
        ....
    }
    

    Any update of a UI element needs to be done in the onPostExecute so if you want to update a textview it needs to be done there.

    the code isnt exactly right but this will get you started to get the idea