gpt4 book ai didi

android - 使用 IBM 示例将 Xml 解析为 android ListView

转载 作者:行者123 更新时间:2023-11-29 02:02:46 24 4
gpt4 key购买 nike

我正在尝试使用 this IBM example 中使用的 XmlPullParser 制作漫画 Rss 阅读器应用程序.我获取了 IBM 源代码,仅将 feedURL 更改为“http://fandom.com/rss/new/manga”,并将所有文件中的 PUB_DATE 字符串更改为 PUBDATE。我仍然无法让应用程序正常运行。

作为引用,我已将源代码重命名为 Otaku,因为我计划将其用作新应用程序的基础。它应该将此 Rss 提要显示为 ListView,但我不明白问题出在哪里。我碰巧是 logcat 文盲:(。我的日志猫状态:

07-29 13:24:17.963: E/Trace(670): error opening trace file: No such file or directory(2)
07-29 13:24:18.113: W/ActivityThread(670): Application com.mypacks.activities.otaku is waiting for the debugger on port 8100...
07-29 13:24:18.133: I/System.out(670): Sending WAIT chunk
07-29 13:24:18.133: I/dalvikvm(670): Debugger is active
07-29 13:24:18.333: I/System.out(670): Debugger has connected
07-29 13:24:18.333: I/System.out(670): waiting for debugger to settle...
07-29 13:24:18.533: I/System.out(670): waiting for debugger to settle...
07-29 13:24:18.733: I/System.out(670): waiting for debugger to settle...
07-29 13:24:18.943: I/System.out(670): waiting for debugger to settle...
07-29 13:24:19.171: I/System.out(670): waiting for debugger to settle...
07-29 13:24:19.386: I/System.out(670): waiting for debugger to settle...
07-29 13:24:19.646: I/System.out(670): waiting for debugger to settle...
07-29 13:24:19.857: I/System.out(670): waiting for debugger to settle...
07-29 13:24:20.089: I/System.out(670): debugger has settled (1518)
07-29 13:24:20.753: I/AndroidNews(670): ParserType=XML_PULL
07-29 13:24:20.903: E/AndroidNews(670): android.os.NetworkOnMainThreadException
07-29 13:24:20.903: E/AndroidNews(670): java.lang.RuntimeException: android.os.NetworkOnMainThreadException
07-29 13:24:20.903: E/AndroidNews(670): at com.mypacks.internals.otaku.XmlPullFeedParser.parse(XmlPullFeedParser.java:57)
07-29 13:24:20.903: E/AndroidNews(670): at com.mypacks.internals.otaku.MessageList.loadFeed(MessageList.java:77)
07-29 13:24:20.903: E/AndroidNews(670): at com.mypacks.internals.otaku.MessageList.onCreate(MessageList.java:33)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.Activity.performCreate(Activity.java:5008)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-29 13:24:20.903: E/AndroidNews(670): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 13:24:20.903: E/AndroidNews(670): at android.os.Looper.loop(Looper.java:137)
07-29 13:24:20.903: E/AndroidNews(670): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-29 13:24:20.903: E/AndroidNews(670): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 13:24:20.903: E/AndroidNews(670): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 13:24:20.903: E/AndroidNews(670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-29 13:24:20.903: E/AndroidNews(670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-29 13:24:20.903: E/AndroidNews(670): at dalvik.system.NativeStart.main(Native Method)
07-29 13:24:20.903: E/AndroidNews(670): Caused by: android.os.NetworkOnMainThreadException
07-29 13:24:20.903: E/AndroidNews(670): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-29 13:24:20.903: E/AndroidNews(670): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-29 13:24:20.903: E/AndroidNews(670): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-29 13:24:20.903: E/AndroidNews(670): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-29 13:24:20.903: E/AndroidNews(670): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-29 13:24:20.903: E/AndroidNews(670): at com.mypacks.internals.otaku.BaseFeedParser.getInputStream(BaseFeedParser.java:36)
07-29 13:24:20.903: E/AndroidNews(670): at com.mypacks.internals.otaku.XmlPullFeedParser.parse(XmlPullFeedParser.java:19)
07-29 13:24:20.903: E/AndroidNews(670): ... 16 more
07-29 13:24:21.173: I/Choreographer(670): Skipped 44 frames! The application may be doing too much work on its main thread.
07-29 13:24:21.223: D/gralloc_goldfish(670): Emulator without GPU emulation detected.

最佳答案

这是您的 logcat 中的重要部分:

Caused by: android.os.NetworkOnMainThreadException

这告诉您您正在尝试发出网络请求以从主线程获取 RSS 提要。

从 Honeycomb (IIRC) 开始,在主线程上执行网络请求会抛出异常,因为平台创建者想提醒您网络操作不应在主线程上进行。

“修复”此问题的快速方法是为较低的 API 级别构建您的应用程序。

更正确的解决方案是将您的网络操作移到它自己的后台线程上。一种方法是将 RSSReader Activity 中的 onCreate() 方法更改为如下所示:

private RSSFeed feed = null;
private Handler h;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Set up a Handler to call updateDisplay() once the feed has been fetched.
h = new Handler(){
@Override
public void handleMessage(Message msg){
// display UI
UpdateDisplay();
}
};


Thread t = new Thread() {
@Override
public void run() {

// go get our feed!
feed = getFeed(RSSFEEDOFCHOICE);
//Send a signal to the handler to tell it that we are done fetching the feed.
h.sendEmptyMessage(0);

}
};
t.start();

}

另一种方法是使用 AsyncTask .

This Lars Vogella blog post是学习线程/处理程序和 AsyncTask 时使用的GREAT资源。

关于android - 使用 IBM 示例将 Xml 解析为 android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11711717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com