2014-01-28 50 views
-1

我想解析來自網頁的原子提要。但第三行顯示錯誤,當我試圖修復這個「它顯示了一個選項:」配置構建路徑「。我該如何修復它?我試圖修復它,但它不是固定的。請幫我解決這個問題從網頁/博客提取原子

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()); 

這是我想你使用的是Rome,要確保你有你的classpath所有羅馬的依賴添加所需的庫來構建路徑,我曾試過

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) 
{ 
} 

代碼}

+0

我想你使用的是羅馬,確保您有所有羅馬的依賴在類路徑中添加所需的庫來構建路徑。 – vzamanillo

+0

我收錄了所有的jdom和rome圖書館,但我不知道我錯了哪裏? – Rose

+0

你使用的是什麼羅馬版本? – vzamanillo

回答

3

您的代碼對我的作品的話,也許你的庫損壞,可以從

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

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

(我推薦使用maven來管理你的項目再次donwload它。 )

然後再次將庫添加到您的構建路徑中,清理您的項目並再次運行它。

由於Google屏蔽了無法識別的HTTP客戶端,因此您的HTTP客戶端需要成爲Chrome,MSIE,Gecko等公認的客戶端,因此將您的HTTP客戶端設置爲User Agent並且可以正常工作。

試試這個代碼:

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); 
     } 

    } 
} 
+0

它顯示403錯誤..你得到的輸出是什麼 – Rose

+0

你知道它的作品... – Rose