我尝试从本教程中学习 RSS Feed 。阅读该教程后,我下载源代码并测试 RSS Feed。但它无法从 RSS 读取图片以在 android 中显示,因为我的 RSS 没有标签media:content但它使用标签附件,例如
<enclosure url="http://p3.s1sf.com/ns/0/ud/238/1192436/1.jpg" type="image/jpeg"/>
当我从教程运行代码时,它在日志中显示如下。
06-19 08:43:57.637: D/rssfeed(785): imageAndTexts1.get(position).getImgLink() :: null ::
所以我在 startElement 函数中检查了XmlHandler.java 。
if (qName.equalsIgnoreCase("media:content"))
{
if (!atts.getValue("url").toString().equalsIgnoreCase("null")) {
feedStr.setImgLink(atts.getValue("url").toString());
} else {
feedStr.setImgLink("");
}
}
我替换为
if (qName.equalsIgnoreCase("enclosure"))
{
if (!atts.getValue("url").toString().equalsIgnoreCase("null")) {
feedStr.setImgLink(atts.getValue("url").toString());
} else {
feedStr.setImgLink("");
}
}
但它显示这样的错误
06-19 08:08:09.165: E/AndroidRuntime(2661): FATAL EXCEPTION: main
06-19 08:08:09.165: E/AndroidRuntime(2661): android.os.NetworkOnMainThreadException
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-19 08:08:09.165: E/AndroidRuntime(2661): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-19 08:08:09.165: E/AndroidRuntime(2661): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-19 08:08:09.165: E/AndroidRuntime(2661): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
06-19 08:08:09.165: E/AndroidRuntime(2661): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
06-19 08:08:09.165: E/AndroidRuntime(2661): at com.amit.adapter.RssReaderListAdapter.getView(RssReaderListAdapter.java:64)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.ListView.makeAndAddView(ListView.java:1831)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.ListView.fillDown(ListView.java:674)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.ListView.fillFromTop(ListView.java:735)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.ListView.layoutChildren(ListView.java:1652)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.View.layout(View.java:14008)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.os.Handler.handleCallback(Handler.java:725)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.os.Looper.loop(Looper.java:137)
06-19 08:08:09.165: E/AndroidRuntime(2661): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-19 08:08:09.165: E/AndroidRuntime(2661): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 08:08:09.165: E/AndroidRuntime(2661): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 08:08:09.165: E/AndroidRuntime(2661): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-19 08:08:09.165: E/AndroidRuntime(2661): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-19 08:08:09.165: E/AndroidRuntime(2661): at dalvik.system.NativeStart.main(Native Method)
我不知道如何从标签媒体:内容和附件中读取 RSS 图像。请帮我。