Search code examples
javaandroidweb-servicesandroid-ksoap2

ksoap2 - Same method works wih AsyncTask but no when using inside normal activity


I'm developing an APP and it has a funcionality that can be used by the user in two different ways: do the task in real time, so he/she have to wait till the task finishes; or do the task in background so he/she can do other things in the meanwhile.

I've developed the task in background with AsyncTask and it works, but I don't know why I'm getting and error when I try to call the same method inside a normal activity.

This is the method that uses ksoap2, the value of the param I'm testing with is "http://www.google.com"

public String sendURL(String url) {
    Log.i("URL", url);
    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("url", url);

    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = false;
    envelope.implicitTypes = true;
    envelope.setAddAdornments(false);
    envelope.setOutputSoapObject(request);

    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
    androidHttpTransport.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");

    SoapPrimitive response = null;
    try {
        //Invoke web service
        androidHttpTransport.call(SOAP_ACTION, envelope);  // HERE is where I'm getting the error
        //Get the response
        response = (SoapPrimitive) envelope.getResponse();
        // Return the response
        return response.toString();
    } catch (SocketException e) {
        e.printStackTrace();
        // TODO Error, el servidor no esta levantado
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return null;
}

This is the output error I'm getting from Android Studio:

03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err: java.net.MalformedURLException
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at java.net.URL.<init>(URL.java:152)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at java.net.URL.<init>(URL.java:125)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at org.ksoap2.transport.ServiceConnectionSE.<init>(ServiceConnectionSE.java:39)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at org.ksoap2.transport.HttpTransportSE.getServiceConnection(HttpTransportSE.java:104)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:61)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at es.sia.urlanalyzer.Analyze.sendURL(Analyze.java:377)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at es.sia.urlanalyzer.Analyze$1.onClick(Analyze.java:167)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at android.view.View.performClick(View.java:5198)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-28 09:25:50.291 17894-17894/es.sia.urlanalyzer W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 09:25:50.292 17894-17894/es.sia.urlanalyzer W/System.err:     at android.os.Looper.loop(Looper.java:148)
03-28 09:25:50.292 17894-17894/es.sia.urlanalyzer W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
03-28 09:25:50.292 17894-17894/es.sia.urlanalyzer W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-28 09:25:50.292 17894-17894/es.sia.urlanalyzer W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-28 09:25:50.292 17894-17894/es.sia.urlanalyzer W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

The same method, exactly the same, is working fine in a custom class that extends AsyncTask. The values of the SOAP calls to the web service are:

String NAMESPACE = "http://10.0.2.2:4894/AnalyzeURL_WS/services/";
String URL = "http://10.0.2.2:4894/AnalyzeURL_WS/services/AnalyzeURL";
String SOAP_ACTION = "http://10.0.2.2:4894/AnalyzeURL_WS/services/analyze";
String METHOD_NAME = "analyze";

Solution

  • The reason to this is quite simple, you cannot run network activities on the main thread/ui thread. Network activities should be run on a background service in order for it not to interfere with U.i processes, if this was allowed then a user would have to wait for the network process to be completed before proceeding with using the app. This would be a bad practise, for example if i want to upload alot of data am fetching from the users message box, and i have 1000 messages, if this process is allowed on ui thread, it would take too much time, but when you run the upload service in the background, the user can comfortable use other features of the app while the data is being uplaoded. Therefore network activities are restricted from taking place in the main thread, if you do so you get a network relate exception.