I’m using the following function to get a JSON Array from a php webpage.
While this exact code works on the 2.3 version of the app, on the 3.0 version that I’m currently building it force-closes and gives me a weird error log.
Any help would be great…
Are there any particularities to Honeycomb as opposed to Gingerbread when using http posts and things like that?
private void getNews(){
try{
HttpGet httpGet = new HttpGet("http://www.ace.ucv.ro/android/stiri.php?perpage=20");
result = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());
JSONArray jsonMainArray = new JSONArray(result);
} catch (ParseException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
And calling the function:
getNews.setOnClickListener(new OnClickListener(){
public void onClick(View view){
getNews();
}
});
And the error log
02-26 16:06:42.218: W/dalvikvm(478): threadid=1: thread exiting with uncaught exception (group=0x40014760)
02-26 16:06:42.247: E/AndroidRuntime(478): FATAL EXCEPTION: main
02-26 16:06:42.247: E/AndroidRuntime(478): android.os.NetworkOnMainThreadException
02-26 16:06:42.247: E/AndroidRuntime(478): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
02-26 16:06:42.247: E/AndroidRuntime(478): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
02-26 16:06:42.247: E/AndroidRuntime(478): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
02-26 16:06:42.247: E/AndroidRuntime(478): at java.net.InetAddress.getAllByName(InetAddress.java:249)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-26 16:06:42.247: E/AndroidRuntime(478): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-26 16:06:42.247: E/AndroidRuntime(478): at pirelli.app.ScanBarcode.getNews(ScanBarcode.java:46)
02-26 16:06:42.247: E/AndroidRuntime(478): at pirelli.app.ScanBarcode.access$4(ScanBarcode.java:42)
02-26 16:06:42.247: E/AndroidRuntime(478): at pirelli.app.ScanBarcode$5.onClick(ScanBarcode.java:164)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.view.View.performClick(View.java:3110)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.view.View$PerformClick.run(View.java:11934)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.os.Handler.handleCallback(Handler.java:587)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.os.Looper.loop(Looper.java:132)
02-26 16:06:42.247: E/AndroidRuntime(478): at android.app.ActivityThread.main(ActivityThread.java:4123)
02-26 16:06:42.247: E/AndroidRuntime(478): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 16:06:42.247: E/AndroidRuntime(478): at java.lang.reflect.Method.invoke(Method.java:491)
02-26 16:06:42.247: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-26 16:06:42.247: E/AndroidRuntime(478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-26 16:06:42.247: E/AndroidRuntime(478): at dalvik.system.NativeStart.main(Native Method)
If you looked for “NetworkOnMainThreadException” in El Goog, the first thing you get is the link to the Android docs. It states that this exception is since Api Level 11 (Honeycomb 3.0), that’s why you were not getting it before. It also even links to the “Designing for Responsiveness” page. The proper way to do network operations is, like some of the commenters suggested, via an AsyncTask.