Search code examples
android.netweb-servicesksoap2socket-timeout-exception

Connecting to web service ends with SocketTimeoutException


I have code segment that connects to a web service. My app has a button, when pressed fetches some data from server and shows to user. It is very simple, but never works. I have read all of post regards to my problem, but none of them solved it. The code is used from codeproject sample:

public void onClick(View v) {
    SoapObject request = new SoapObject("http://tempuri.org/", "Add");
    PropertyInfo pi=new PropertyInfo();
    pi.setName("a");
    pi.setValue(10);
    pi.setType(Integer.class);
    request.addProperty(pi);
    pi=new PropertyInfo();
    pi.setName("b");
    pi.setValue(15);
    pi.setType(Integer.class);
    request.addProperty(pi);

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

    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE("http://grasshoppernetwork.com/NewFile.asmx");
    String response="";
    try
    {
        httpTransport.call("http://tempuri.org/Add", envelope);
        response = (envelope.getResponse()).toString();
    }
    catch (Exception exception)
    {
        response=exception.toString();
    }


    onSoapRequestFinished(response.toString());
}

I have added 2 permissions: Network_Access and Internet. Surprisingly, the web service is accessible from AVD's browser.

The errors are:

java.net.MalformedURLException: no protocol: java.net.SocketTimeoutException
    at java.net.URL.<init>(URL.java:589)
    at java.net.URL.<init>(URL.java:486)
    at java.net.URL.<init>(URL.java:435)
    at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:115)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:155)
    at ir.dana.sharif.testxml.MainActivity.onSoapRequestFinished(MainActivity.java:103)
    at ir.dana.sharif.testxml.MainActivity.onClick(MainActivity.java:84)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Solution

  • I post the answer for future readers. After investigating many methods i found that the imported library for KSoap was wrong. I used ksoap.jar for my app which couldn't connect to server. I replaced it with ksoap2-android-assembly-2.6.0-jar-with-dependencies and anything goes well as expected.

    Additionally, volley library works as charm.