Search code examples
androidjsonweb-servicesksoap2

Problems calling SOAP-JSON web service from android


Hi I'm using Eclipse to develop over android and trying to connect to a .Net webservice. The webservice method have the parameters "usuario" and "password" and it returns a boolean.

I'm using ksoap2 library to call the webservice but I'm having problems, my application crush when I try to call the webservice.

When I see the response header of the variable "transporte" it says "HTTP/1.1 500 Internal Server Error" so I think maybe I'm having problems with the parameters or something. I already allowed the internet connection permission to Android Manifest.

And I'm running the app on the emulator.

Here is my code:

public class EntrarActivity extends Activity {

///---Constantes para la invocación del servicio---
private static final String METHOD_NAME = "existeUsuario";
private static final String SOAP_ACTION = WebServiceConstants.NAMESPACE + EntrarActivity.METHOD_NAME;

///---Declaración de variables para consumir el servicio---
private SoapObject request;
private SoapSerializationEnvelope envelope;
private SoapPrimitive resultsRequestSoap;

///---Declaracion de variable para serializar/deserializar
Gson gson;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entrar);
}

public void clicked_loginButton(View view)
{
    String usuario = ((EditText)findViewById(R.id.editText_mail_entrar)).getText().toString();
    String password = ((EditText)findViewById(R.id.editText_password_entrar)).getText().toString();

    request = new SoapObject(WebServiceConstants.NAMESPACE, EntrarActivity.METHOD_NAME);

    //---le agregamos los parametros---
    request.addProperty("usuario", usuario);
    request.addProperty("password", password);

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true; //por que es .NET

    envelope.setOutputSoapObject(request);

    HttpTransportSE transporte = new HttpTransportSE(WebServiceConstants.URL);

    try {

        transporte.call(EntrarActivity.SOAP_ACTION, envelope);
        resultsRequestSoap = (SoapPrimitive)envelope.getResponse();

    } catch (IOException e) {

        e.printStackTrace();

    } catch (XmlPullParserException e) {

        e.printStackTrace();

    }

    String strJson = resultsRequestSoap.toString();
}
}

Solution

  • Try downloading SoapUI and building the request in SoapUI first. It allows you to easily tweak the input - this will help you narrow it down to a problem in your SOAP request code, or a response from the web service. Once you get the request working in SoapUI, you can dump your Java SOAP request to a string and compare the two. Chances are, you're forgetting something in the SOAP request you are building. SoapUI will query the WSDL and build a sample request by adding the service:

    http://<hostname>/webservice?WSDL
    

    Compare the output generated by the template to what you are sending to the web service.