-1

我在 Android 2.3.5 和 Android 4 中运行相同的代码。但在第 4 版中,互联网似乎无法正常工作。但是当我检查手机中的浏览器时,它可以工作。

HttpGet httpget = new HttpGet("http://www.blah.com/?"+paramString);
            try {
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                //------- scrape data
                if (entity != null) {

以下是错误日志

06-06 12:25:48.685: E/AndroidRuntime(7972): FATAL EXCEPTION: main
06-06 12:25:48.685: E/AndroidRuntime(7972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.superhub.matkahupi/com.superhub.matkahupi.AutodetectedTripsActivity}: android.os.NetworkOnMainThreadException
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.os.Looper.loop(Looper.java:137)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at java.lang.reflect.Method.invoke(Method.java:511)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at dalvik.system.NativeStart.main(Native Method)
06-06 12:25:48.685: E/AndroidRuntime(7972): Caused by: android.os.NetworkOnMainThreadException
06-06 12:25:48.685: E/AndroidRuntime(7972):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-06 12:25:48.685: E/AndroidRuntime(7972):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
4

2 回答 2

1

你的主线程无法处理它所以把它放在AsyncTask中。所以你必须在其他线程中调用 http 请求

于 2012-06-06T09:35:39.360 回答
1
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.superhub.matkahupi/com.superhub.matkahupi.AutodetectedTripsActivity}: android.os.NetworkOnMainThreadException

Android 4.0 严格禁止在主线程上进行网络操作。正如 Samir 所说,在非 UI 上执行网络或任何操作需要超过 5 秒才能执行。

于 2012-06-06T09:54:29.090 回答