I'm trying to access a web service using ksoap2 inside an asynctask. I wrote this code:
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.AsyncTask;
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);
hotelNetwork task = new hotelNetwork();
task.execute(new String[]{NAMESPACE, URL, SOAP_ACTION, METHOD_NAME});
}
@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;
}
private class hotelNetwork extends AsyncTask<String[], Void, String> {
@Override
protected String doInBackground(String[]... params) {
String[] data = params[0];
String namespace = data[0];
String url = data[1];
String action = data[2];
String method = data[3];
String result = "";
SoapObject request = new SoapObject(namespace, method);
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(action, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
TextView tv = (TextView) MainActivity.this.findViewById(R.id.textView);
tv.setText(result);
MainActivity.this.setContentView(tv);
}
}
}
The textView still showes the default text "Hello World" when I ran the app instead of the response from the web service. I debugged it and saw that the doinBackground method actually gets the response and returns it but the onPostExecute is not called. What may be the cause of this? I heard that this might be caused by an exception in the doinBackground method but it does not show an exception. Any help would be appreciated.
Remove MainActivity.this.setContentView(tv);
basically what you are doing is creating the default view again that says "Hello World" after you already set the new text