From your code, it seems the network operation is inside the onClickListener, accessed on the UI thread / main thread of the application.
Check O.S. version on emulator and device:
The reason why your application would crash on Android versions 3.0 and above, but would work fine on Android 2.x is because Honeycomb and Ice Cream Sandwich(and onwards), the O.S. are much stricter about abuse against the UI Thread. For example, when an Android device running HoneyComb or above detects a network access on the UI thread, a NetworkOnMainThreadException
will be thrown:
E/AndroidRuntime(673): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example/com.example.ExampleActivity}: android.os.NetworkOnMainThreadException
The explanation as to why this occurs is well documented on the Android developer's site:
A
NetworkOnMainThreadException
is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.
Go through:
Why the app would crash or work depending on O.S.
Try AsyncTask to avoid NetworkOnMainThread
Why should you not use the Strict Mode alternative as your solution and only to debug(i'd suggest avoid that also actually, you know what is the problem now):
Critical to fix it, not by setting Thread policies