SOAP call from Android with KSoap2 to .net WebServer

263 Views Asked by At

Good day everyone.

I'm trying to do a SOAP call to a .net WebServer running locally, but I think I'm missing something. The Android code seems fine to me. Maybe the SOAP request is wrong...

This is what the webserver gives for the method I'm trying to call:

POST /WebAvanza/WebAvanzaInterface.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <InitTransazione xmlns="http://tempuri.org/">
      <ip>string</ip>
      <codice>string</codice>
    </InitTransazione>
  </soap12:Body>
</soap12:Envelope>

and this is what i wrote so far:

private String InitTransaction(String command){
        String NAMESPACE = "http://tempuri.org/";
        String METHOD_NAME = "InitTransazione";
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("ip", ipAddress);
        request.addProperty("codice", command);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        httpTransport.debug = true;

        String SOAP_ACTION = NAMESPACE + METHOD_NAME;

        Object result = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            result = envelope.getResponse();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return String.valueOf(result);
    }

URL is http://192.168.168.24/WebAvanza/WebAvanzaInterface.asmx, the address refers to the machine where the server is running.

I only get an exception after executing httpTransport.call.

This is the StackTrace (if anybosy needs it)

W/System.err: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1528)
W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:389)
W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
W/System.err:     at java.net.Socket.connect(Socket.java:621)
W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:145)
W/System.err:     at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:141)
W/System.err:     at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
W/System.err:     at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
W/System.err:     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:130)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.sendData(HttpTransportSE.java:295)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:184)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
W/System.err:     at com.infovision.elcam_02.Operations$override.InitTransaction(Operations.java:64)
W/System.err:     at com.infovision.elcam_02.Operations$override.InitOperation(Operations.java:40)
W/System.err:     at com.infovision.elcam_02.Operations$override.access$dispatch(Unknown Source:49)
W/System.err:     at com.infovision.elcam_02.Operations.InitOperation(Unknown Source:15)
2

There are 2 best solutions below

1
Juanjo Berenguer On

Checking your code I think the main problem is because you are not executing Okhttpclient in async thread.

I think the problem should be solved if you run the code using enqueue method of the Okhttpclient.

client.newCall(request).enqueue(new Callback() {
      @Override public void onFailure(Call call, IOException e) {
        //Exception here
        }

      @Override public void onResponse(Call call, Response response) throws IOException {
        try (ResponseBody responseBody = response.body()) {
           /*
           * Handle your response here :
           * Do whatever you want if the response.isSuccesful or not
           */ 
        }
    });

I hope it helps you.

0
Oiproks On

I found out that the problem was in the server and the code was fine... Goddamn I spent almost 5 hours behind this .__.