Search code examples
androidmultithreadingandroid-asynctaskandroid-ksoap2

Android: trouble calling web service using AsyncTask


The code below usaually works good, but, If the code is executing and the connections is suddenly lost, an error often occurs. The error that I reported at bottom of this question seems to start near the line 430, that is:

androidHttpTransport.call(soap_action, envelope); 

The following is the AsyncTask that do my work:

  private class CheckUpdateTask extends AsyncTask<Void, Void, ArrayList<String>>{
            private SoapObject response = null;
            ArrayList<String> methodsResult = new ArrayList<String>();      
    @Override
        protected ArrayList<String> doInBackground(Void... params) {
            // TODO Auto-generated method stub
                SoapObject Request = new SoapObject(NAMESPACE,"CheckUpdate");
                Request.addProperty("Username", username);
                Request.addProperty("Password", password);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(Request);
            final String soap_action = NAMESPACE+"CheckUpdate";

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(soap_action, envelope); //line 430, where error occurs!

            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
                            }

            try {
                response = (SoapObject)envelope.getResponse();
                ...here I use the returned data for build my array (methodsResult)

            SoapObject pi = (SoapObject)response.getProperty(0);
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return methodsResult; 
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        // TODO Auto-generated method stub
        String[] resultArr= result.toArray(new String[result.size()]);
        new callWS().execute(resultArr);

        super.onPostExecute(result);
    }
  }

And here my error message:

05-07 12:58:47.920: E/AndroidRuntime(31053): FATAL EXCEPTION: AsyncTask #5
05-07 12:58:47.920: E/AndroidRuntime(31053): java.lang.RuntimeException: An error occured while executing doInBackground()
05-07 12:58:47.920: E/AndroidRuntime(31053):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.lang.Thread.run(Thread.java:856)
05-07 12:58:47.920: E/AndroidRuntime(31053): Caused by: java.lang.NullPointerException
05-07 12:58:47.920: E/AndroidRuntime(31053):    at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:85)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:167)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at host.framework.ServicePromemoria$CheckUpdateTask.doInBackground(ServicePromemoria.java:430)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at host.framework.ServicePromemoria$CheckUpdateTask.doInBackground(ServicePromemoria.java:1)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-07 12:58:47.920: E/AndroidRuntime(31053):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-07 12:58:47.920: E/AndroidRuntime(31053):    ... 5 more

I'm not sure that the problem is related to leak of connectivity.


Solution

  • Check that none of your variables are set to null. If not, and the problem is http call simply handle the exception.

    try { 
        androidHttpTransport.call(soap_action, envelope); //line 430, where error occurs!
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (Exception e) {
        enter code here
    }
    

    Hope it helps!