I need to make a call to a soap webservice in an android device. I have been reading a lot of articles in stackoverflow and on other pages, watching videos... But I've tried everythin and I can't make it work in my android device, and I can't test in on an emulator, because my computer can't handle any of them, so I don't know if the error is on the code or if it's a problem of my android device.
The layout xml are only an EditText, a Button, and a TextView.
In this link you can see the request xml I need to send to the webservice (should I use SOAP 1.1 or SOAP 1.2 ?) http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry
This is my actual code, and I have tried many other ways, and none of them have worked for me. Any help? (url, namespace, soap_action and method_name values are okey, aren't they?)
package com.example.doazdoas.webservice_prueba;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.widget.Toast;
import static android.content.ContentValues.TAG;
public class MainActivity extends Activity{
private TextView textResult;
private Button buttonSend;
String NAMESPACE = "http://www.webserviceX.NET/";
String METHOD_NAME = "GetCitiesByCountry";
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://www.webservicex.net/globalweather.asmx?WSDL";
private Object resultsRequestSOAP = null;
HttpTransportSE androidHttpTransport;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textResult = (TextView)findViewById(R.id.textResultado);
buttonSend = (Button)findViewById(R.id.buttonEnviar);
//setContentView(tv);
}
public void onClickEnviar(View view){
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
sendRequest();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
Log.d("dump Request: " ,androidHttpTransport.requestDump);
Log.d("dump response: " ,androidHttpTransport.responseDump);
}
}
public void sendRequest(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//SoapObject
request.addProperty("@CountryName", "SPAIN");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
resultsRequestSOAP = envelope.getResponse();
String[] results = (String[]) resultsRequestSOAP;
textResult.setText( results[0]);
}
catch (Exception aE)
{
aE.printStackTrace ();
}
}
}