我的应用程序在Android版本2.3.3中工作没有任何问题但是当我在Galaxy S5(4及更多版本)上测试它时,它不起作用。
这是LogCat错误输出:
10-17 04:18:10.265: W/dalvikvm(26757): threadid=1: thread exiting with uncaught exception (group=0x41a95c08)
10-17 04:18:10.265: E/AndroidRuntime(26757): FATAL EXCEPTION: main
10-17 04:18:10.265: E/AndroidRuntime(26757): Process: com.androidexample.httppostexample, PID: 26757
10-17 04:18:10.265: E/AndroidRuntime(26757): android.os.NetworkOnMainThreadException
10-17 04:18:10.265: E/AndroidRuntime(26757): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
10-17 04:18:10.265: E/AndroidRuntime(26757): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-17 04:18:10.265: E/AndroidRuntime(26757): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-17 04:18:10.265: E/AndroidRuntime(26757): at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1227)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-17 04:18:10.265: E/AndroidRuntime(26757): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-17 04:18:10.265: E/AndroidRuntime(26757): at com.androidexample.httppostexample.HttpPostExample$2.onClick(HttpPostExample.java:157)
10-17 04:18:10.265: E/AndroidRuntime(26757): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
10-17 04:18:10.265: E/AndroidRuntime(26757): at android.os.Handler.dispatchMessage(Handler.java:102)
10-17 04:18:10.265: E/AndroidRuntime(26757): at android.os.Looper.loop(Looper.java:136)
10-17 04:18:10.265: E/AndroidRuntime(26757): at android.app.ActivityThread.main(ActivityThread.java:5586)
10-17 04:18:10.265: E/AndroidRuntime(26757): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 04:18:10.265: E/AndroidRuntime(26757): at java.lang.reflect.Method.invoke(Method.java:515)
10-17 04:18:10.265: E/AndroidRuntime(26757): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
10-17 04:18:10.265: E/AndroidRuntime(26757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
10-17 04:18:10.265: E/AndroidRuntime(26757): at dalvik.system.NativeStart.main(Native Method)
这是我的AndroidManifest
任何人都可以帮我解决吗?
答案 0 :(得分:0)
好吧,所以马上看错误你可以看到问题。您正在主线程上进行联网。出于正当理由,这是不允许的,因为它会严重影响您的UI。现在有两种方法可以解决这个问题。
将您的网络请求放在不同的线程中,例如Async任务(如果您需要帮助,请发表评论)
关闭此功能。不推荐,但我想我至少会告诉你:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分:-1)
您正尝试在主线程中运行Network。你试过添加吗?
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder(),permitAll().build();
StrictMode.setThreadPolicy(policy);
}
以下是指向HTTP帖子的完整sample code的链接。