0
votes

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

2

2 Answers

3
votes

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(); 
0
votes

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);  
    }