3

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

10
  • 1
    please, Show your Logcat output as well as show your service code Commented Apr 2, 2014 at 7:02
  • better to call that webservice in AsyncTask. Commented Apr 2, 2014 at 8:39
  • I know AsyncTask is better option, but i want to sync many records that were previously not synced due to network problem. So whenever network will be available i am calling web service in background thread. If i use AsyncTask then user will not be able to do UI interaction until it finishes. Commented Apr 2, 2014 at 8:47
  • but i had searched on google but didn't found exact cause of your issue. so told you to use AsyncTask. what UI operation you wanna do while calling web service.? Commented Apr 2, 2014 at 8:49
  • Any UI interaction, not any specific.. Anyways if no other option is there, at the end that's the only solution..To do it with AsyncTask.. Commented Apr 2, 2014 at 8:53

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

2 Comments

Ok.. I will try it :)
Yes, It's working amazing now.. :) I have searched for this issue for long time but did not get any solution.. Your suggestion totally solved my issue..Thanks a lot..
0

Please provide your code. You just start the background service and in onStart method of service call the method to invoke web service and stop the service after the back ground process get completed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.