0

我尝试从本教程中学习 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 图像。请帮我。

4

3 回答 3

0

这与您解析 rss 的方式无关。错误是“NetworkOnMainThreadException”所以这意味着你试图在主线程上获取这个 rss,Android 平台不允许访问主线程上的网络以防止锁定用户的事件。您必须在后台线程上调用此 rss 阅读方法...

new Thread(new Runnable() { 
        @Override
        public void run() {
            ReadRss();
        } 
    }).start();
于 2014-02-03T14:14:45.110 回答
0

你通过 rss 提要获取一个 url,然后像这样将 url 传递给这个函数

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

` 最后你得到一个图像然后你把它设置到 imageview 像这样 Imageview.setImageBitmap

于 2013-06-19T09:10:14.273 回答
0

您必须使用 using 在主线程上使用此下载操作AsyncTask。看到这个

于 2015-12-25T10:38:58.920 回答