0

My program is working absolutely fine from android 2.1 to android 2.3

But its not working on higher version of android

I have used in my manifest file

<uses-sdk android:minSdkVersion="7"  
      android:targetSdkVersion="10" 
           android:maxSdkVersion="15"/>

Logcat output error while running the project on android 4.0.3 avd results in

05-09 12:45:12.051: E/AndroidRuntime(530): FATAL EXCEPTION: main` 05-09 12:45:12.051: E/AndroidRuntime(530): java.lang.RuntimeException: Unable to start activity ComponentInfo{giv.home/giv.home.Start}: android.os.NetworkOnMainThreadException 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread.access$600(ActivityThread.java:123) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.os.Handler.dispatchMessage(Handler.java:99) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.os.Looper.loop(Looper.java:137) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-09 12:45:12.051: E/AndroidRuntime(530): at java.lang.reflect.Method.invokeNative(Native Method) 05-09 12:45:12.051: E/AndroidRuntime(530): at java.lang.reflect.Method.invoke(Method.java:511) 05-09 12:45:12.051: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-09 12:45:12.051: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-09 12:45:12.051: E/AndroidRuntime(530): at dalvik.system.NativeStart.main(Native Method) 05-09 12:45:12.051: E/AndroidRuntime(530): Caused by: android.os.NetworkOnMainThreadException 05-09 12:45:12.051: E/AndroidRuntime(530): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 05-09 12:45:12.051: E/AndroidRuntime(530): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 05-09 12:45:12.051: E/AndroidRuntime(530): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 05-09 12:45:12.051: E/AndroidRuntime(530): at java.net.InetAddress.getAllByName(InetAddress.java:220) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-09 12:45:12.051: E/AndroidRuntime(530): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 05-09 12:45:12.051: E/AndroidRuntime(530): at giv.home.ConnectionClass.connectToServer(ConnectionClass.java:41) 05-09 12:45:12.051: E/AndroidRuntime(530): at giv.home.Start.onCreate(Start.java:64) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.Activity.performCreate(Activity.java:4465) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-09 12:45:12.051: E/AndroidRuntime(530): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-09 12:45:12.051: E/AndroidRuntime(530): ... 11 more

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
sidG
  • 49
  • 1
  • 1
  • 9
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – laalto Nov 08 '13 at 08:21

2 Answers2

3

I think this is because you're trying to use HTTPConnect in the main thread... Android no longer supports using HTTP in the main thread and requires that you do it in a background thread. Just guessing, of course... but that's what I suspect is happening.

However, if your target SDK version is 10, then why not just skip setting the max? I mean... what is it about ICS that you feel will be the absolute highest level that your app can run on? the min and target are totally sufficient, I believe. The point of setting a Max is so that (for example) your app won't be installable on ICS now that ICS is available and you know there's something about it that your app doesn't support. So, in that case you'd set 13 and only Honeycomb users (and lower) would be able to use it. Since 15 is the highest version available, setting it is premature... 16 might be just as capable of handling your app as all the rest, for all you know! :)

[EDIT]

You don't even need an AsyncTask (though that would be the more Androidy way to do it). For now just use a regular Thread...

 Handler h = new Handler();
 private Runnable stuffToDoAfterPost = new Runnable(){
       public void run(){
            //whatever you wish to happen after you have done your server call
       }
 };

private class CallServer extends Thread{
   public void run(){
      //make your server call here...


      //then...
      h.post(stuffToDoAfterPost);
   }
}

new CallServer().start(); 
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • Then why is my project running from sdk 7 to 10. The same error is showing up even when i set the target to 15 and do away with the maxSdkVersion – sidG May 09 '12 at 07:45
  • if my guess is correct < 10 was ok with running HTTPConnections on the main display thread. Honeycomb and above doesn't allow it. What actual version of the SDK are you compiling into your app now? Is it still 2.x? or did you move it up to 3.x or 4.x? – Yevgeny Simkin May 09 '12 at 07:48
  • I tested it on android avd 4.0.3, android 3.2 i haven't tested as the avd i am getting is of a tablet. – sidG May 09 '12 at 07:51
  • @sidG check this: [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Paresh Mayani May 09 '12 at 07:52
  • I have built a ConnectionClass i am unable to understand where do i implement the AsyncTask – sidG May 09 '12 at 08:12
0

To resovled StrictMode issue you need to use below code in your activity -

static{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  
    StrictMode.setThreadPolicy(policy);  
    }
Rakesh
  • 2,732
  • 29
  • 28