集册 Java实例教程 表示USGS/Atom馈送

表示USGS/Atom馈送

欢马劈雪     最近更新时间:2020-01-02 10:19:05

539
代表USGS / Atom提要



/*
时代Java公众号 - nowjava.com
*/

import java.net.URL;

import java.io.PrintWriter;

import java.util.List;

import java.util.ArrayList;

import java.util.Date;

import java.text.DateFormat;

import java.text.SimpleDateFormat;


import com.sun.syndication.feed.synd.SyndFeed;

import com.sun.syndication.feed.synd.SyndEntry;

import com.sun.syndication.feed.synd.SyndFeedImpl;

import com.sun.syndication.io.SyndFeedOutput;

import com.sun.syndication.io.SyndFeedInput;

import com.sun.syndication.io.XmlReader;//来 自 时   代     Java  公  众  号 - nowjava.com


//extra stuff needed by atom feed

import com.sun.syndication.feed.synd.SyndLinkImpl;

import com.sun.syndication.feed.synd.SyndCategoryImpl;

import com.sun.syndication.feed.synd.SyndContentImpl;


public class FeedAtom {

    private SyndFeed feed = null;

    private SyndFeedOutput outFeed = null;


    Date lastUpdated = new Date();

    Date mostRecent = new Date();


    private List entries = new ArrayList();


    String[] urls = {

    "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.atom" };


    public FeedAtom() {

        feed = new SyndFeedImpl();

        feed.setFeedType("atom_0.3"); //not sure of v0.3 or v1.0

        feed.setTitle("Atom Aggregated Feed");

        feed.setDescription("Combine Atom feeds for Twitter Bot.");

        feed.setAuthor("None");

        feed.setLink("http://www.example.com");

    }


    /** *************************************************

     * This creates a news list with all the newest entries since

     * last run of parseFeed().

     *

     * ************************************************** */

    public void parseFeed() {

        entries = new ArrayList();

        feed.setEntries(entries);


        for (int i = 0; i < urls.length; ++i) {

            URL inputURL = null;

            SyndFeedInput input = null;

            SyndFeed inFeed = null;


            try {

                inputURL = new URL(urls[i]);

                input = new SyndFeedInput();

                inFeed = input.build(new XmlReader(inputURL));


                //entries.addAll(inFeed.getEntries());


                //create a whole new list and add the new items to it.

                for (SyndEntry entry : (List<SyndEntry>) inFeed

                        .getEntries()) {

                    if (entry.getUpdatedDate().after(lastUpdated)) {

                        entries.add(entry);


                        //find the most recent entry (like finding the max value)

                        if (entry.getUpdatedDate().after(mostRecent)) {

                            mostRecent = entry.getUpdatedDate();

                        }

                    }

                }


            } catch (Exception ex) {

                ex.printStackTrace();

                System.out.println("ERROR (line 95): " + ex.getMessage());

            }

        }


        lastUpdated = mostRecent;

    } //end parseFeed()


    public boolean isUpdated() {

        return !entries.isEmpty();

    }


    public Date getLastUpdate() {

        return lastUpdated;

    }


    public List<SyndEntry> getEntries() {

        return entries;

    }


    //tester/client

    public static void main(String args[]) {

        DateFormat df = new SimpleDateFormat("yyyy-M-d HH:mm:ss z");

        FeedAtom myfeed = new FeedAtom();

        List<SyndLinkImpl> links = null;


        while (true) {

            myfeed.parseFeed();

            if (myfeed.isUpdated()) {

                System.out.println("Most recent entry: "

                        + df.format(myfeed.lastUpdated));

                for (SyndEntry entry : myfeed.getEntries()) {

                    System.out.println("Title: " + entry.getTitle());

                    System.out.println("URI: " + entry.getUri());

                    System.out.println("Updated Date: "

                            + df.format(
展开阅读全文