Search code examples
androidksoap2android-ksoap2ksoap

Android getting error Ksoap in android 4 or above


My application could running in android 2.* without any problem and that can correctly use KSoap module. but after installing app in Android 4 or above i get this error:

627-627/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
            at java.net.InetAddress.getAllByName(InetAddress.java:220)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
            at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
            at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
            at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
            at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
            at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
            at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
            at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
            at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
            at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
            at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
            at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
            at ir.tsms.wsdl.WSDLHelper.call(WSDLHelper.java:34)
            at ir.tsms.wsdl.TSMS.checkUserAuth(TSMS.java:64)

My WSDLHEPLER is:

public class WSDLHelper {
    public static String call(SoapObject request) throws TException {
        String result;

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

        AndroidHttpTransport transport = new AndroidHttpTransport(Strings.URL_TSMS);
        transport.debug = true;

        try {

            /*  GETTING ERROR IN BELOW LINE  */
            transport.call(Strings.URL_TSMS + request.getName(), envelope);
            result = envelope.getResponse().toString();
        } catch (IOException ex) {
            Log.e("" , ex.getMessage());
            throw new TException(PublicErrorList.NOT_EXIST_ERROR_DETAIL);
        } catch (XmlPullParserException ex) {
            Log.e("" , ex.getMessage());
            throw new TException(PublicErrorList.NOT_EXIST_ERROR_DETAIL);
        }
        if (result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE)))
            throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);

        return result;
    }

    public static String convert1(String str) {
        return str.substring(1, str.length() - 1);
    }

    public static String[] convert2(String str) {
        return str.split(", ");
    }

    public static String[] convert3(String str) {
        return str.split(",");
    }

}

after testing in android 2.2,3,3.* not problem. i dont know why this module could not running in android 4. please help me. thanks

my Ksoap is :

ksoap2-android-assembly-2.4-jar-with-dependencies.jar

Solution

  • If the other answer does not work, then try this in your onCreate method,

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