0

My App work in Android Version 2.3.3 Without any problem but when i testdc it on Galaxy S5 ( 4 and more ), it does not work.

here is the LogCat Error output:

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)

This is my AndroidManifest

Can anyone help me to solve it?

display name
  • 4,165
  • 2
  • 27
  • 52

2 Answers2

0

Alright, so right away looking at the error you can see the problem. You are doing networking on your main thread. This is not allowed, for good reason, as it would severely impact your UI. Now there are two ways to fix this.

  1. Put your network request in a different thread, for example an Async task (comment if you need help doing that)

  2. Turn OFF this feature. NOT RECOMMENDED but I thought I would at least tell you:

.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Colin
  • 491
  • 1
  • 5
  • 13
  • Hi Coli. can you check my code? – Alireza Ghasempour Oct 17 '15 at 01:23
  • If you are implementing the bad solution (which again, you should not do). All you need to do is throw those two lines in the onCreate function and import the proper library (Android Studio should do for you) – Colin Oct 17 '15 at 01:25
-1

You are trying to run Network in the main thread. Have you tried adding this?

if (android.os.Build.VERSION.SDK_INT > 9) {

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

  StrictMode.setThreadPolicy(policy);

}

Here is a link to a complete sample code for HTTP post.

display name
  • 4,165
  • 2
  • 27
  • 52