-1

I run the same code in Android 2.3.5 and in Android 4. But in the version 4, the internet seems to be not working. But when i check the browser in the phone it works.

HttpGet httpget = new HttpGet("http://www.blah.com/?"+paramString);
            try {
                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                //------- scrape data
                if (entity != null) {

and following is the error log

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)
dinesh707
  • 12,106
  • 22
  • 84
  • 134
  • Solution is here [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Paresh Mayani Jun 06 '12 at 09:42
  • 1
    You're RIGHT! Android 4 broke the internet and Android 5 plans to break our highway and banking systems (also it will make anyone who uses it sterile!!). RUN FOR YOUR LIVES!!! – Yevgeny Simkin Jun 06 '12 at 09:57

2 Answers2

1

your main thread can't handle it so put it in AsyncTask.so you have to call http request in other thread

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.superhub.matkahupi/com.superhub.matkahupi.AutodetectedTripsActivity}: android.os.NetworkOnMainThreadException

Android 4.0 Strictly forbids doing network operations on main thread. As Samir said perform network or any operations on non-UI which takes more than 5 seconds to execute.

Vipul
  • 27,808
  • 7
  • 60
  • 75