【问题标题】:Android Rss Image ProblemAndroid Rss 图片问题
【发布时间】:2011-05-16 20:06:50
【问题描述】:

大家好 我对 Android 开发比较陌生,我正在为新闻网站创建一个 RSS 阅读器。 我遇到的问题是带有我想要获取图像的 rss 提要的站点的类型是“我为 android 上的代码做的是 ...2 个类 ...RssItem 和 RssItem Displayer

public class RssItem {

private String title;
private String description;
private Date pubDate;
private String link;
private static ImageView image;

public RssItem(String title, String description,ImageView image, Date pubDate, String link) {
    this.title = title;
    this.description = description;
    RssItem.image = image;
    this.pubDate = pubDate;
    this.link = link;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
public ImageView getImage(ImageView image) {
      return this.image = image;
}     
public void setImage(ImageView image) {
          this.image = image;     
}
public Date getPubDate() {
    return pubDate;
}

public void setPubDate(Date pubDate) {
    this.pubDate = pubDate;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

@Override
public String toString() {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");

    String result = getTitle() + "   ( " + sdf.format(this.getPubDate()) + " )";
    return result;
}

public static ArrayList<RssItem> getRssItems(String feedUrl) {

    ArrayList<RssItem> rssItems = new ArrayList<RssItem>();

    try {
        //open an URL connection make GET to the server and 
        //take xml RSS data
        URL url = new URL(feedUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream();

            //DocumentBuilderFactory, DocumentBuilder are used for 
            //xml parsing
            DocumentBuilderFactory dbf = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            //using db (Document Builder) parse xml data and assign
            //it to Element
            Document document = db.parse(is);
            Element element = document.getDocumentElement();

            //take rss nodes to NodeList
            NodeList nodeList = element.getElementsByTagName("item");

            if (nodeList.getLength() > 0) {
                for (int i = 0; i < nodeList.getLength(); i++) {

                    //take each entry (corresponds to <item></item> tags in 
                    //xml data

                    Element entry = (Element) nodeList.item(i);

                    Element _titleE = (Element) entry.getElementsByTagName(
                            "title").item(0);
                    Element _descriptionE = (Element) entry
                            .getElementsByTagName("description").item(0);
                    Element _imageE = (Element) entry
                            .getElementsByTagName("image").item(0);
                    Element _pubDateE = (Element) entry
                            .getElementsByTagName("pubDate").item(0);
                    Element _linkE = (Element) entry.getElementsByTagName(
                            "link").item(0);

                    String _title = _titleE.getFirstChild().getNodeValue();
                    String _description = _descriptionE.getFirstChild().getNodeValue();

                     // ImageView image = (ImageView)findViewbyId(R.id.MyImage);
                     // Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
                     // image.setImageBitmap(bitmap); 
                    //} catch (MalformedURLException e) {

                    /*try {
                          //where imageUrl is what you pulled out from the rss feed
                          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
                          image.setImageBitmap(bitmap); 
                        } catch (MalformedURLException e) {
                         //log exception here
                        } catch (IOException e) {
                          //log exception here
                        }

                    */

                    Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
                    String _link = _linkE.getFirstChild().getNodeValue();

                    //create RssItemObject and add it to the ArrayList
                    RssItem rssItem = new RssItem(_title, _description, image,
                            _pubDate, _link);

                    rssItems.add(rssItem);
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rssItems;
}      

但是不知道src图片元素怎么放

我还创建了一个将在布局 xml 上显示的图像视图

在显示 RSS 项目显示器的另一个类中,我有

public class RssItemDisplayer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss_item_displayer);

    RssItem selectedRssItem = com.AndroidRSSReader.AndroidRSSReader.selectedRssItem;
    //Bundle extras = getIntent().getExtras();
    TextView titleTv = (TextView)findViewById(R.id.titleTextView);  
    TextView contentTv = (TextView)findViewById(R.id.contentTextView);  
    ImageView image=(ImageView)findViewById(R.id.MyImage);  

    String title = "";
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");
    title = "\n" + selectedRssItem.getTitle() + "   ( "
     + sdf.format(selectedRssItem.getPubDate()) + " )\n\n";

    String content = "";
    content += selectedRssItem.getDescription() + "\n"
            + selectedRssItem.getLink();

    titleTv.setText(title);
    contentTv.setText(content);

    image=selectedRssItem.getImage(image);
    try {
          String feedUrl = null;
        //where imageUrl is what you pulled out from the rss feed
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(feedUrl).getContent());
          image.setImageBitmap(bitmap); 
        } catch (MalformedURLException e) {
         //log exception here
        } catch (IOException e) {
          //log exception here
        }

logcat 给了我在代码中获取位图的 rss 项目行的警告

11-29 00:13:44.593: WARN/System.err(2997): java.net.MalformedURLException: Protocol not found: 
11-29 00:13:44.593: WARN/System.err(2997):     at java.net.URL.<init>(URL.java:275)
11-29 00:13:44.593: WARN/System.err(2997):     at java.net.URL.<init>(URL.java:159)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.RssItem.getRssItems(RssItem.java:92)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.AndroidRSSReader.refressRssList(AndroidRSSReader.java:301)
11-29 00:13:44.593: WARN/System.err(2997):     at com.AndroidRSSReader.AndroidRSSReader.onCreate(AndroidRSSReader.java:229)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 00:13:44.593: WARN/System.err(2997):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 00:13:44.603: WARN/System.err(2997):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 00:13:44.603: WARN/System.err(2997):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 00:13:44.613: WARN/System.err(2997):     at android.os.Looper.loop(Looper.java:123)
11-29 00:13:44.613: WARN/System.err(2997):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 00:13:44.613: WARN/System.err(2997):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 00:13:44.613: WARN/System.err(2997):     at java.lang.reflect.Method.invoke(Method.java:521)
11-29 00:13:44.613: WARN/System.err(2997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
11-29 00:13:44.613: WARN/System.err(2997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
11-29 00:13:44.613: WARN/System.err(2997):     at dalvik.system.NativeStart.main(Native Method)

我应该在这两件事中添加什么来获取图像,尤其是 rss 项目类 无论如何,如果有人可以提供帮助,我将不胜感激

【问题讨论】:

  • 不清楚您所说的“但我不知道如何放置 src 图像元素”是什么意思。你能更好地描述一下吗?此外,您的一些问题在应该有的时候没有被格式化为代码,这使得它难以阅读。
  • RSS 提要有这种格式的图像
  • 我明白这一点。问题是什么?我的猜测是您想要获取该图像 URL 并将其显示在 android.widget.ImageView 中。对吗?
  • 是的,就是这样......我应该在元素和创建 RSS 项目对象的部分中放置什么
  • XML 只是文本,src 属性只包含图像的 URL。您只需将图像字段设置为字符串,并将图像下载为其他位置的位图(最好在 AsyncTask 中以防止 UI 阻塞)。至于获取 src 属性,您将想要获取 img 元素,并在其上调用 .getAttribute("src")

标签: java android image rss


【解决方案1】:

您的 RssItem 类不应包含以下内容:

private static ImageView image;

替换为:

private String imageUrl;

在您的getRssItems 方法中,使用_imageE 获取imageUrl 的值。然后使用imageUrl,如下所述。

ImageView 只会显示存储在您设备上的本地图像,因此将其设置为远程 URL 将不起作用。可以在这里找到一种解决方案:Android, Make an image at a URL equal to ImageView's image 所以,你会想要改变:

titleTv.setText(title);
image.setImageURI(uri);
contentTv.setText(content);

到:

titleTv.setText(title);
contentTv.setText(content);
try {
  //where imageUrl is what you pulled out from the rss feed
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  image.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
 //log exception here
} catch (IOException e) {
  //log exception here
}

【讨论】:

  • 我试图把你的答案放到我的代码中,但它把它改成了位图......我不知道如何实现它。我想在我输入的新 URL(imageUrl) 部分feedUrl ....
  • 是的,但这是针对 RSS Diplayer 类的,我认为它不正确Rss Displayer 类,其中图像将实际显示在布局中(Imageview),Url 必须再次设置 ....
  • 任何人,请帮助伙计们并不难,我想
猜你喜欢
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多