1

我正在尝试制作一个显示我网站上的最新消息的 android 应用程序,我对这两件事特别感到困惑?

1.每次手机自动联网时,我如何才能从我的网站获取新闻?

2.我将如何存储这些信息并随后将其用于设备上的更新?

我从谷歌找到了这段代码,我是否朝着正确的方向前进?:

    public abstract class BaseFeedParser implements FeedParser {

    // names of the XML tags
    static final String PUB_DATE = "pubDate";
    static final  String DESCRIPTION = "description";
    static final  String LINK = "link";
    static final  String TITLE = "title";
    static final  String ITEM = "item";

    final URL feedUrl;

    protected BaseFeedParser(String feedUrl){
        try {
            this.feedUrl = new URL(feedUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            return feedUrl.openConnection().getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
4

2 回答 2

1

您找到的这段代码是一种类似于 XML 解析器的方法。基本上,它应该做的是从您的网站接收和解析信息。可以在此处找到有关在 Android 中解析 XML 的更多信息。

回答您的问题:

  1. 您可以通过其 RSS 提要从您的网站获取新闻。

    这不是一项微不足道的任务。你应该:

    • 使用指向您网站的 RSS 链接发出HTTP请求,以获取 XML 提要作为输入流。HttpURLConnection你可以通过课堂来实现这一点。更多细节在这里。确保将<uses-permission android:name="android.permission.INTERNET" />权限添加到AndroidManifest.xml

    • 获得 XML 后,使用提要解析器对其进行解析并获取RssItems. 一个RssItem可能看起来像这样:

      public class RSSItem {
      
          private String title;
          private String description;
          private String category;
          private String pubdate;
          private String link;
      
         //Getters and setters here
      }
      
    • 现在您应该有 aList<RssItem>并显示 a 中的项目ListView
  2. 您可以将一堆存储RSSItemSQLite Database. 您的数据库可能只有一个表,其中包含 5 个项目属性(标题、描述等)作为列 + 一列作为 id。更多关于数据库的信息在这里

关于every time the phone is connected to internet automatically

从用户的角度来看,我不希望 RSS 新闻应用程序在每次手机连接到 Internet 时都不断地获取提要。这是因为性能问题(电池消耗、数据流量)。如果手机始终连接到 Internet,在此期间它不会获取任何内容怎么办?

相反,我希望这种应用程序具有刷新机制,以便在我想要的时候获取新闻(也许实现一个按钮)。

于 2012-09-07T22:21:52.290 回答
0

You could use a BroadcastReceiver that get's notified whenever a connectivity change occurs. See more about this here:

http://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/

http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/

But you probably end up making too many requests - because the user will use your app only occasionally, while the BroadcastReceiver gets notified of every change.

Since you are displaying a feed why not load the data when the user actually uses your app and until they the newest feed items are available present the existing news items.

I would prefer an app that loads data at startup over an app that drains the battery of my device any time - unless I use the app and the up-to-date data constantly (like email).

As another possibility you might want to have a look at Google Cloud Messaging. In this case changes are pushed in a battery-friendly way.

For your second question: For a feed I probably would use the SQLite database available on all Android devices.

于 2012-09-07T20:22:18.720 回答