0

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.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
user2742861
  • 340
  • 1
  • 8
  • 17

3 Answers3

1

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

Community
  • 1
  • 1
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
1

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;
Naeem A. Malik
  • 995
  • 4
  • 19
  • `final Object result = (Object)envelope.getResponse(); String `strRet = result.toString(); // to get return value – Naeem A. Malik Sep 27 '13 at 11:16
  • `final Object result = (Object)envelope.getResponse(); String strRet = result.toString(); TextView txt = (TextView)findViewById(R.id.textView1); txt.setText(strRet);` But it's not giving any value. It's giving an Orange message: `09-27 07:30:40.094: W/System.err(3171): java.lang.ClassCastException: java.lang.String cannot be cast to org.ksoap2.serialization.SoapPrimitive` – user2742861 Sep 27 '13 at 11:31
  • sorry, I don't know what's the problem this time round. I've checked and double checked, same code is working for me. Besides, the error sounds like you're trying to make a SoapPrimitive out of a String somewhere. – Naeem A. Malik Sep 27 '13 at 11:58
1

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); 
Vamshi
  • 1,495
  • 1
  • 15
  • 31