Вопрос

I am trying to parse atom feeds from a webpage. But the third line shows error and when I tried to fix this "it shows an option :"configure build path". How can I fix it?I tried to fix it but it's not get fixed.Please help me to fix this

URL feedUrl = new URL("http://localhost:8080/namespace/feed/");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
System.out.println("Feed Title: " + feed.getTitle());

This is the code Which I had tried

try {
URL url = new URL("https://www.google.com/search?hl=en&q=robbery&tbm=blg&
output=atom");SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(url));
System.out.println("Feed Title: " + feed.getTitle());
for (SyndEntry entry : (List<SyndEntry>) feed.getEntries())
{
System.out.println("Title: " + entry.getTitle());
System.out.println("Unique Identifier: " + entry.getUri());
System.out.println("Updated Date: " + entry.getUpdatedDate());
for (SyndLinkImpl link : (List<SyndLinkImpl>) entry.getLinks())
{
System.out.println("Link: " + link.getHref());}           
for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents())
{
System.out.println("Content: " + content.getValue());
}


for (SyndCategoryImpl category : (List<SyndCategoryImpl>) entry.getCategories())
{
System.out.println("Category: " + category.getName());
}
}//for
}//try
catch (Exception ex) 
{
}

}

Это было полезно?

Решение

I suppose you are using Rome, be sure you have all Rome dependencies in your classpath adding the required libraries to build path.

Your code works for me so, maybe your libraries are corrupt, you can donwload it again from

http://mvnrepository.com/artifact/rome/rome/1.0

and

http://mvnrepository.com/artifact/jdom/jdom/1.0

(I recommend use maven to manage your project.)

then add the libraries to your build path again, clean your project and run it again.

You are getting a HTTP 403 error code because Google blocks the unrecognized HTTP clients, your HTTP client needs to be a recognized client like Chrome, MSIE, Gecko... etc, set the User Agent to your HTTP client and will work.

Try this code:

import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndLinkImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

public class Rome {

    public static void main(String[] args) {
        try {
            URLConnection urlConnection = new URL("https://www.google.com/search?hl=en&q=robbery&tbm=blg&output=atom").openConnection();
            urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

            SyndFeedInput input = new SyndFeedInput();
            input.setPreserveWireFeed(true);
            SyndFeed feed = input.build(new XmlReader(urlConnection));
            System.out.println("Feed Title: " + feed.getTitle());
            for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
                System.out.println("Title: " + entry.getTitle());
                System.out.println("Unique Identifier: " + entry.getUri());
                System.out.println("Updated Date: " + entry.getUpdatedDate());
                for (SyndLinkImpl link : (List<SyndLinkImpl>) entry.getLinks()) {
                    System.out.println("Link: " + link.getHref());
                }
                for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents()) {
                    System.out.println("Content: " + content.getValue());
                }

                for (SyndCategoryImpl category : (List<SyndCategoryImpl>) entry.getCategories()) {
                    System.out.println("Category: " + category.getName());
                }
            }// for
        }// try
        catch (Exception ex) {
            System.err.println(ex);
        }

    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top