今天遇到1个错,添入wooboo的广告的app在Android4.0 device上不能显示广告,总是报这个错。

09-19 20:41:40.942: D/libc-netbsd(16786): getaddrinfo: ade.wooboo.com.cn  return error = 0x8 >>
09-19 20:41:40.942: W/System.err(16786): android.os.NetworkOnMainThreadException
09-19 20:41:40.943: W/System.err(16786):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-19 20:41:40.943: W/System.err(16786):     at java.net.InetAddress.lookupHostByName(InetAddress.java:392)
09-19 20:41:40.943: W/System.err(16786):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
09-19 20:41:40.944: W/System.err(16786):     at java.net.InetAddress.getAllByName(InetAddress.java:221)
09-19 20:41:40.944: W/System.err(16786):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
09-19 20:41:40.944: W/System.err(16786):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-19 20:41:40.944: W/System.err(16786):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
09-19 20:41:40.945: W/System.err(16786):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
09-19 20:41:40.945: W/System.err(16786):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-19 20:41:40.945: W/System.err(16786):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
09-19 20:41:40.945: W/System.err(16786):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
09-19 20:41:40.945: W/System.err(16786):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
09-19 20:41:40.946: W/System.err(16786):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
09-19 20:41:40.946: W/System.err(16786):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-19 20:41:40.946: W/System.err(16786):     at com.wooboo.adlib_android.sc.c(sc.java)
09-19 20:41:40.946: W/System.err(16786):     at com.wooboo.adlib_android.sc.a(sc.java)
09-19 20:41:40.946: W/System.err(16786):     at com.wooboo.adlib_android.sc.a(sc.java)
09-19 20:41:40.947: W/System.err(16786):     at com.wooboo.adlib_android.sc.a(sc.java)
09-19 20:41:40.947: W/System.err(16786):     at com.wooboo.adlib_android.ImpressionAdView.f(ImpressionAdView.java)
09-19 20:41:40.947: W/System.err(16786):     at com.wooboo.adlib_android.ImpressionAdView.v(ImpressionAdView.java)
09-19 20:41:40.948: W/System.err(16786):     at com.wooboo.adlib_android.e.handleMessage(e.java)
09-19 20:41:40.948: W/System.err(16786):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 20:41:40.948: W/System.err(16786):     at android.os.Looper.loop(Looper.java:137)
09-19 20:41:40.948: W/System.err(16786):     at android.app.ActivityThread.main(ActivityThread.java:4624)
09-19 20:41:40.948: W/System.err(16786):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 20:41:40.949: W/System.err(16786):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 20:41:40.949: W/System.err(16786):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-19 20:41:40.950: W/System.err(16786):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-19 20:41:40.950: W/System.err(16786):     at dalvik.system.NativeStart.main(Native Method)

经过1番搜索,这个链接给出的解答:http://stackoverflow.com/questions/6976317/android-http-conncetion-exception

在为honeycomb以上 target的应用中,有些涉及到network比较费时的动作是不能放在Main Thread中做的,也就是报NetworkOnMainThreadException的原因所在。

解决方法有3:

  1. 将这些动作移到其余Thread中实现
  2. 将AndroidManifest.xml中的android:targetSdkVersion改为10(不包含10)以下,或者删掉它
  3. disable StrictMode to neglect Network Operation
    protected static void setPermissiveThreadPolicy() {
        // set StrictMode to allow network/disk in service
        StrictMode.ThreadPolicy oldThreadPolicy = StrictMode.getThreadPolicy();
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder(oldThreadPolicy).
                permitNetwork().
                permitDiskReads().
                permitDiskWrites().
                build()
        );
    }

ref:

  1. http://stackoverflow.com/questions/9535229/ice-cream-sandwich-strictmode
  2. http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html
Advertisements