我尝试从这个tutorial学习RSS Feed。阅读完该教程后,我下载源代码并测试RSS Feed。但它无法从RSS中读取图片以显示在Android中,因为我的RSS没有标记 media:content 但它使用标记 enclosure ,例如
<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标记媒体中读取图像:内容和附件。请帮我。
答案 0 :(得分:0)
你通过rss feed获取一个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.setImageBitmap
的imageview答案 1 :(得分:0)
如何解析rss这不是一个错误。错误是“NetworkOnMainThreadException”所以这意味着你试图在主线程上获取这个rss,Android平台不允许在主线程上访问网络以防止锁定用户的事件。你必须在后台线程上调用这个rss读取方法......
new Thread(new Runnable() {
@Override
public void run() {
ReadRss();
}
}).start();
答案 2 :(得分:0)
您必须使用AsyncTask
在主线程上使用此下载操作。请参阅this。