user2742861
user2742861

Reputation: 340

Android SOAP wsdl

I'm trying to connect to a Webservice from SOAP. I developed the SOAP Webservice in PHP and it's working fine (tried in Visual Studio).

First of all, this is the error:

09-27 06:28:07.724: E/AndroidRuntime(2057): Caused by: android.os.NetworkOnMainThreadException
09-27 06:28:07.724: E/AndroidRuntime(2057):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.io.IoBridge.connect(IoBridge.java:112)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at java.net.Socket.connect(Socket.java:842)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at com.example.FirstExample.CategoriesActivity.onCreate(CategoriesActivity.java:49)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at android.app.Activity.performCreate(Activity.java:5133)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-27 06:28:07.724: E/AndroidRuntime(2057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-27 06:28:07.724: E/AndroidRuntime(2057):     ... 11 more

The full error you can view here: http://pastebin.com/cRQ66vrj

And this is my code in Java:

private final String NAMESPACE = "http://10.0.0.20/soap/test_wsdl";
private final String URL = "http://10.0.0.20/info_send/encode.php?wsdl";
private final String SOAP_ACTION = "http://10.0.0.20/info_send/encode.php/select_data";
private final String METHOD_NAME = "select_data";
private String Webresponse = "";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response;
            response = (SoapPrimitive)envelope.getResponse();
            Webresponse = response.toString();
        } catch (SoapFault e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        Log.e("VALUE: ", Webresponse);

I've been following several tutorials, but none of them adapt to my needs. So I'm trying to create a method which does what I want. Also, I added to Manifest:

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>

What Am I doing wrong? Also, if you could point me a direction so I could get item by item the values retrieved with the code above, I would thank you. Something like:

for(int i = 0; i <= variable.size; i++){
   Log.e("Value: ", variable[i].ToString())
}

Thanks.

Upvotes: 0

Views: 1114

Answers (3)

Vamshi
Vamshi

Reputation: 1495

Use AsyncTask to do network related operations...

Below is another solution: Just add below lines in your code.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

Upvotes: 1

Naeem A. Malik
Naeem A. Malik

Reputation: 1025

First of all, the exception clearly tells what's wrong with your code

Caused by: android.os.NetworkOnMainThreadException

You cannot perform long running network operations on the main thread. Move at least the last method call to an asynchronous method, you can do it like this,

ExecutorService mExec = Executors.newSingleThreadExecutor();

    mExec.execute(new Runnable() {          
                @Override
                public void run() {             
                    try{
        androidHttpTransport.call(SOAP_ACTION, envelope);
                    }catch(Exception exc){
                        exc.printStackTrace();
                    }
                }
            });

I had to add this line to make ksoap2 work with my web-services androidHttpTransport.setXmlVersionTag(K.STR_XML_VERSION);

I also set following SOAP envelope properties

        envelope.encodingStyle = SoapEnvelope.ENV;
        envelope.setAddAdornments(false);
        envelope.implicitTypes = false;

Upvotes: 1

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

from your logs

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)

Be sure that your SOAP request runs in AsyncTask (or in Service).

If you run it in main Thread (aka in Activity) it can lead to above mentioned error.

Information about AsyncTask you can found here

and here some example

Upvotes: 1

Related Questions