Ksoap2在android 3.0中无法运行NetworkOnMainThreadException

时间:2011-02-24 15:32:39

标签: android ksoap2 android-strictmode

Ksoap2无法在Android 3.0中运行。 Logcat显示NetworkOnMainThreadException:

02-24 20:18:34.536: ERROR/AndroidRuntime(428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com. Bill/com. Bill.Diag}: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1701)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1717)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:984)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.os.Looper.loop(Looper.java:126)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.main(ActivityThread.java:3900)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at java.lang.reflect.Method.invoke(Method.java:491)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at dalvik.system.NativeStart.main(Native Method)
02-24 20:18:34.536: ERROR/AndroidRuntime(428): Caused by: android.os.NetworkOnMainThreadException
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1069)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at java.net.Socket.connect(Socket.java:901)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at com.IsBill.Connection.Connect(Connection.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at com.IsBill.Diagnos.FirstProv(Diagnos.java:385)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at com.IsBill.Diagnos.onCreate(Diagnos.java:53)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1665)
02-24 20:18:34.536: ERROR/AndroidRuntime(428):     ... 11 more

3 个答案:

答案 0 :(得分:4)

在HoneyComb中启用了StrictMode,禁用它以避免NetworkOnMainThreadException

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

<强>更新

其他方式检测是使用StrictModeWrapper类存在strictMode。

 private static boolean strictModeAvailable;

    // use the StrictModeWrapper to see if we are running on Android 2.3 or higher and StrictMode is available
    static {
        try {
            StrictModeWrapper.checkAvailable();
            strictModeAvailable = true;
        } catch (Throwable throwable) {
            strictModeAvailable = false;
        }
    }

StrictModeWrapper Class

import android.os.StrictMode;

/**
 * StrictModeWrapper is a wrapper class for the android class android.os.StrictMode provided with Android 2.3 onwards.
 * It allows usage of StrictMode class on devices/emulators with Android 2.3 or higher, while providing an availability
 * check so that the code can stay in situ for lower platform versions. See the application class for usage.
 *
 * @author Manfred Moser <manfred@simpligility.com>
 *
 * @see "http://android-developers.blogspot.com/2009/04/backward-compatibility-for-android.html"
 * @see "http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html"
 */
public class StrictModeWrapper {

    /* class initialization fails when this throws an exception */
   static {
       try {
           Class.forName("android.os.StrictMode", true, Thread.currentThread().getContextClassLoader());
       } catch (Exception ex) {
           throw new RuntimeException(ex);
       }
   }

    /**
     * Check if the class android.os.StrictMode is available at runtime.
     */
   public static void checkAvailable() {}


    /**
     * Call StrictMode.enableDefaults().
     */
    public static void enableDefaults() {
       StrictMode.enableDefaults();
    }

    // all the implementation below is not tested but it should work ... feel free to check and send me fixes..
    public static  void setThreadPolicy(android.os.StrictMode.ThreadPolicy policy) {
        StrictMode.setThreadPolicy(policy);
    }


    public static  android.os.StrictMode.ThreadPolicy getThreadPolicy() {
        return StrictMode.getThreadPolicy();
    }

    public static  android.os.StrictMode.ThreadPolicy allowThreadDiskWrites() {
        return StrictMode.allowThreadDiskWrites();
    }

    public static  android.os.StrictMode.ThreadPolicy allowThreadDiskReads() {
        return StrictMode.allowThreadDiskReads();
    }

    public static  void setVmPolicy(android.os.StrictMode.VmPolicy policy) {
        StrictMode.setVmPolicy(policy);
    }

    public static android.os.StrictMode.VmPolicy getVmPolicy() {
        return StrictMode.getVmPolicy();
    }

    public static final class ThreadPolicyWrapper
    {
        public static final class BuilderWrapper
        {
            StrictMode.ThreadPolicy.Builder builderInstance;

            public  BuilderWrapper() {
                    builderInstance = new StrictMode.ThreadPolicy.Builder();
            }

            public  BuilderWrapper(android.os.StrictMode.ThreadPolicy policy) {
                    builderInstance = new StrictMode.ThreadPolicy.Builder(policy);
            }

            public android.os.StrictMode.ThreadPolicy.Builder detectAll() {
                return builderInstance.detectAll();
            }

            public android.os.StrictMode.ThreadPolicy.Builder permitAll() {
                return builderInstance.permitAll();
            }

            public android.os.StrictMode.ThreadPolicy.Builder detectNetwork() {
                return builderInstance.detectNetwork();
            }

            public android.os.StrictMode.ThreadPolicy.Builder permitNetwork() {
                return builderInstance.permitNetwork();
            }

            public android.os.StrictMode.ThreadPolicy.Builder detectDiskReads() {
                return builderInstance.detectDiskReads();
            }

            public android.os.StrictMode.ThreadPolicy.Builder permitDiskReads() {
                return builderInstance.permitDiskReads();
            }

            public android.os.StrictMode.ThreadPolicy.Builder detectDiskWrites() {
                return builderInstance.detectDiskWrites();
            }

            public android.os.StrictMode.ThreadPolicy.Builder permitDiskWrites() {
                return builderInstance.permitDiskWrites();
            }

            public android.os.StrictMode.ThreadPolicy.Builder penaltyDialog() {
                return builderInstance.penaltyDialog();
            }

            public android.os.StrictMode.ThreadPolicy.Builder penaltyDeath() {
                return builderInstance.penaltyDeath();
            }

            public android.os.StrictMode.ThreadPolicy.Builder penaltyLog() {
                return builderInstance.penaltyLog();
            }

            public android.os.StrictMode.ThreadPolicy.Builder penaltyDropBox() {
                return builderInstance.penaltyDropBox();
            }

            public android.os.StrictMode.ThreadPolicy build() {
                return builderInstance.build();
            }
        }
    }

    public static final class VmPolicyWrapper {
        public static final class BuilderWrapper {
            private StrictMode.VmPolicy.Builder builderInstance;

            public BuilderWrapper() {
                builderInstance = new StrictMode.VmPolicy.Builder();
            }

            public android.os.StrictMode.VmPolicy.Builder detectAll() {
                return builderInstance.detectAll();
            }

            public android.os.StrictMode.VmPolicy.Builder detectLeakedSqlLiteObjects() {
                return builderInstance.detectLeakedSqlLiteObjects();
            }

            public android.os.StrictMode.VmPolicy.Builder penaltyDeath() {
                return builderInstance.penaltyDeath();
            }

            public android.os.StrictMode.VmPolicy.Builder penaltyLog() {
                return builderInstance.penaltyLog();
            }

            public android.os.StrictMode.VmPolicy.Builder penaltyDropBox() {
                return builderInstance.penaltyDropBox();
            }

            public android.os.StrictMode.VmPolicy build() {
                return builderInstance.build();
            }
        }
    }
    // add more wrapping as desired..
}

答案 1 :(得分:0)

您需要在单独的线程中进行任何网络操作,而不是在UI线程上进行。最简单的方法是使用AsyncTask。

答案 2 :(得分:0)

网络操作必须处理新线程才能这样做 您应该在AsyncTask任务类中调用ksoap请求 在你的Asynctask中用doInBackground()方法调用你的KSOAP reuest e.g。

public class Get extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg) {

   //your KSOAP request code
}

}