本文介绍了Android的错误解析XML - android.os.NetworkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到以下错误......
I am getting following error.....
08-18 00:02:19.230: D/CLIPBOARD(17993): Hide Clipboard dialog at Starting input: finished by someone else... !
08-18 00:02:21.515: E/Connectivity(17993): true
08-18 00:02:21.535: D/AndroidRuntime(17993): Shutting down VM
08-18 00:02:21.535: W/dalvikvm(17993): threadid=1: thread exiting with uncaught exception (group=0x40c3c1f8)
08-18 00:02:21.545: E/AndroidRuntime(17993): FATAL EXCEPTION: main
08-18 00:02:21.545: E/AndroidRuntime(17993): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.yipl.rssfeed/org.yipl.rssfeed.News}: android.os.NetworkOnMainThreadException
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.os.Looper.loop(Looper.java:137)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread.main(ActivityThread.java:4507)
08-18 00:02:21.545: E/AndroidRuntime(17993): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 00:02:21.545: E/AndroidRuntime(17993): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 00:02:21.545: E/AndroidRuntime(17993): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-18 00:02:21.545: E/AndroidRuntime(17993): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-18 00:02:21.545: E/AndroidRuntime(17993): at dalvik.system.NativeStart.main(Native Method)
08-18 00:02:21.545: E/AndroidRuntime(17993): Caused by: android.os.NetworkOnMainThreadException
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-18 00:02:21.545: E/AndroidRuntime(17993): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-18 00:02:21.545: E/AndroidRuntime(17993): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-18 00:02:21.545: E/AndroidRuntime(17993): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.yipl.rssfeed.XMLParser.getXmlFromUrl(XMLParser.java:46)
08-18 00:02:21.545: E/AndroidRuntime(17993): at org.yipl.rssfeed.News.onCreate(News.java:50)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.Activity.performCreate(Activity.java:4465)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-18 00:02:21.545: E/AndroidRuntime(17993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
08-18 00:02:21.545: E/AndroidRuntime(17993): ... 11 more
我试着用logcat的跟踪误差及以下code发现错误:
I tried to trace the error using logcat and found error in following code:
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);//I Found error here in this line...
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
Log.e("ErrorPositionTestiong", xml);
return xml;
}
我的这个计划是XML解析功能的一部分。我不明白那是什么错误意味着..请帮我..谢谢提前。
My this program is a part of XML parsing function.. I dont understand what that error means..Please help me.. thanks in advance.
推荐答案
这是一个严格的模式冲突。总之,你不应该做任何网络或数据库运行在主线程。使用AsyncTask的用于获取XML。
This is a Strict-mode violation. In short, you are not supposed to do any network or DB operation on the main thread. Use an AsyncTask for getting the XML.
这篇关于Android的错误解析XML - android.os.NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!