Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.IOException While invoking webService from Service

I am invoking web service from my application. I have used Service instead of ASyncTask to invoke the web service. I am getting java.io.IOException. I have tested with AsyncTask also but its working fine. Problem is only when i use Service(background thread). Please help me to resolve this issue.

Logcat is

04-02 17:01:30.103: W/System.err(17131): java.io.IOException
04-02 17:01:30.104: W/System.err(17131):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:87)
04-02 17:01:30.105: W/System.err(17131):    at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:205)
04-02 17:01:30.105: W/System.err(17131):    at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:109)
04-02 17:01:30.106: W/System.err(17131):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
04-02 17:01:30.107: W/System.err(17131):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
04-02 17:01:30.108: W/System.err(17131):    at com.example.rajneta.Syncdetails.send_voter_record(Syncdetails.java:204)
04-02 17:01:30.108: W/System.err(17131):    at com.example.rajneta.Syncdetails.onStartCommand(Syncdetails.java:62)
04-02 17:01:30.109: W/System.err(17131):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2638)
04-02 17:01:30.110: W/System.err(17131):    at android.app.ActivityThread.access$1900(ActivityThread.java:149)
04-02 17:01:30.111: W/System.err(17131):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-02 17:01:30.112: W/System.err(17131):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 17:01:30.113: W/System.err(17131):    at android.os.Looper.loop(Looper.java:153)
04-02 17:01:30.114: W/System.err(17131):    at android.app.ActivityThread.main(ActivityThread.java:4987)
04-02 17:01:30.115: W/System.err(17131):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 17:01:30.116: W/System.err(17131):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 17:01:30.117: W/System.err(17131):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-02 17:01:30.118: W/System.err(17131):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-02 17:01:30.119: W/System.err(17131):    at dalvik.system.NativeStart.main(Native Method)

Code :

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    send_voter_record();
    return Service.START_NOT_STICKY;
}

public void send_voter_record() {
    SoapObject request = null;
    try {
        String query;
        request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("data", query);
    } catch (Exception e) {
        e.printStackTrace();
    }
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    System.out.println("###########request" + request);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;
    envelope.encodingStyle = SoapEnvelope.XSD;
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Object root = envelope.getResponse();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Thanks in advance

like image 245
Snehal Avatar asked May 21 '26 20:05

Snehal


1 Answers

Basically Services is an application component that can perform long-running operations in the background and does not provide a user interface. And AsyncTask allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Working with web-services, you have to call it Asynchronously, because asynchronous invocation enables Web service clients to initiate a request to a Web service, continue processing without blocking, and receive the response at some point in the future.

So,

As per your app requirement, You have to call web-service using Services. But what you have done is, You are calling your web-service direct in Services, instead of that, just call your web-service into asynctask and call async task from service. Thats all.

Get back with your feedback.

Happy coding...

like image 136
Shailesh Avatar answered May 24 '26 12:05

Shailesh