Bartosz Bialecki
Bartosz Bialecki

Reputation: 4451

SocketTimeoutException in android using ksoap2

I saw similar questions but I didn't find an answer. I'm using ksoap2 library to connect with the webserver and sometimes I got that exception java.net.SocketTimeoutException: read timed out.

Here is my code:

    SoapObject request = new SoapObject(NAMESPACE, method);

    if (properties != null) {
        for (PropertyInfo property : properties) {
            request.addProperty(property);
        }
    }

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

    HttpsTransportSE transport = new HttpsTransportSE(HOST, PORT, FILE, TIMEOUT);

    transport.debug = true;
    transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

    try {
        transport.call(NAMESPACE + "#" +  soapAction, envelope);

        Object res = (Object)envelope.getResponse();

        return res;
    } catch (Exception e) {
        Log.e("WebService", e.toString());

        return null;
    }

Can I change somewhere the timeout for socket or what can I do?

Upvotes: 1

Views: 11794

Answers (10)

Pealy
Pealy

Reputation: 1

2019 Update!

After spending a long time trying to resolve my problems with this I would like to post my findings:

  • Worked 100% fine in emulator but could not make it work on my phone at all. (although emulator needed IP address for service on my network, it could not resolve the machine name)

  • When running on phone the calls always produced a SocketTimeoutException instantly after the androidHttpTransport.call(soap_action,envelope) line was executed - there was no 'wait' even though I would set a timeout like this:

    HttpTransportSE androidHttpTransport = new HttpTransportSE(wsdl_url,40000000)

  • I had been using v2.5.2 of kSOAP and thanks to mya in a previous response I changed to ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar. This did not fix the problem but it did change the error message provided to Cleartext HTTP traffic not permitted

  • This issues could then be fixed easily by adding this line to the Application section of the AndoidManifest.xml:

    android:usesCleartextTraffic="true"

Also I had to disable Google Play Protect to allow installation from APK

ZIP of my project in case you want to see. It includes a public internet web service (http://www.dneonline.com/calculator.asmx) which should work for anyone. The other button calls a local service so will fail for you. There are lots of commented out lines that I needed to get me to this point - to be ignored!

Upvotes: 0

Mohammad Roshani
Mohammad Roshani

Reputation: 769

interesting, while increasing/decreasing target APIs and playing around with manifest, please make sure you have the internet permission:

  <uses-permission android:name="android.permission.INTERNET" />

Android KSOAP2 throws SocketTimeoutException

Upvotes: 0

mya
mya

Reputation: 81

if anyone still encounter this, my case is changing from ksoap2-android-2.5.2.jar to ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar stops throwing SocketTimeoutException

Upvotes: 0

Ehsan Eslami
Ehsan Eslami

Reputation: 1

add bellow code in OnCreate:

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

Upvotes: -2

RAJESH KUMAR ARUMUGAM
RAJESH KUMAR ARUMUGAM

Reputation: 1570

I got SocketTimeOutExceptions because of the following reasons:

  • Invalid NAMESPACE,METHOD_NAME,SOAP_ACTION,URL
  • Network Connectivity Issues

Adding

HttpTransportSE androidhttpsTransportSE=new HttpTransportSE(URL,600000);

or

 HttpTransportSE androidhttpsTransportSE=new HttpTransportSE(URL);

both doesn't make any big difference.

Finally, I fixed it with the Help of Async task As Suggested by Liaqat.For more Details Check my GitHub project for SOAP Demo using Ksoap2

Upvotes: 0

Sandoval0992
Sandoval0992

Reputation: 1367

I had the exact same problem using ksoap2 library when making a client app to consume a web service, what you must do is to perform all network access operations in an AsyncTask.

I'm my case, the timeout_in_milliseconds parameter that someone suggests to pass to the HttpTransportSE object constructor didn't work for me.

HttpTransportSE httpTransport = new HttpTransportSE(URL, timeout_in_millsecond);

If you've never used async tasks before, follow the link below to the android documentation so that you can get started.

AsyncTask documentation

Upvotes: 0

Shaishav Jogani
Shaishav Jogani

Reputation: 2121

You can control the timeout exception by providing average time that your web service needs to respond.

HttpTransportSE httpTransport = new HttpTransportSE(URL, timeout_in_millsecond);

Upvotes: 4

dreamdeveloper
dreamdeveloper

Reputation: 1276

Make sure of the below two, 1) HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000); 60000 - timeout value 2) Use http://10.0.2.2 instead of http://localhost

Upvotes: 0

icke
icke

Reputation: 1578

I'm not sure if this is the same problem I just had. There were multiple calls and on a real device only the first one would work, while the rest would fail with a timeout.

Disconnecting after each call solved the problem for me:

transport.call(NAMESPACE + "#" +  soapAction, envelope);
transport.getServiceConnection().disconnect();

Upvotes: 2

Riteeka Chhabra
Riteeka Chhabra

Reputation: 31

The Socket times out either when the service is not responding or there could be network connection problem. In my case When service started responding i did'nt got Socket Time Out Exception and hence the problem was solved. Here is my code :

public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException 
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
    // adding a property to SoapObject
    PropertyInfo pi = new PropertyInfo();
    FileDetailList C = new FileDetailList();
    pi.setName("C");
    pi.setValue(C);
    pi.setType(C.getClass());
    request.addProperty(pi);
    //  request.addProperty("iTopN", "5"); //variable name, value. I got the variable name, from the wsdl file!
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
    envelope.setOutputSoapObject(request);  //prepare request
    HttpTransportSE httpTransport = new HttpTransportSE(URL);
    //  AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);  
    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump)
    httpTransport.call(SOAP_ACTION, envelope); //send request
    //  SoapObject result=(SoapObject)envelope.getResponse();
    SoapObject result=(SoapObject)envelope.bodyIn; //get response
    return result;          
}

Upvotes: 3

Related Questions