我在 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)