我正在使用以下函数从php网页获取JSON数组。
虽然这个确切的代码适用于2.3版本的应用程序,但是我正在构建的3.0版本强制关闭并给出了一个奇怪的错误日志。
任何帮助都会很棒......
使用http帖子和类似的东西时,Honeycomb是否有任何特殊性而不是Gingerbread?
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();
}
}
调用函数:
getNews.setOnClickListener(new OnClickListener(){
public void onClick(View view){
getNews();
}
});
错误日志
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)
答案 0 :(得分:3)
如果您在El Goog中查找“NetworkOnMainThreadException”,您首先得到的是Android docs的链接。它声明这个例外是自Api Level 11(Honeycomb 3.0)以来,这就是你之前没有得到它的原因。它甚至还链接到“设计响应性”页面。正如一些评论者建议的那样,通过AsyncTask进行网络操作的正确方法。