2014-05-24 67 views

回答

0

我認爲你可以將這個PHP類到的java類輕鬆使用它:

<?php 
** 
* Google-News feed parser and JSON provider Class 
* 
* @package None 
* @author Nuhil Mehdy <[email protected]> 
*/ 
class GoogleNews { 

    public $searchQuery = 'Good News'; 

    public function __construct ($searchQuery) { 
      if(!empty($searchQuery)) { 
        $this->searchQuery = $searchQuery; 
      } 

      $this->numberOfNews = 5; 
    } 

    public function __toString() { 
      return $this->getNews(); 
    } 

    public function setSearchQuery($searchQuery) { 
      if(!empty($searchQuery)) { 
        $this->searchQuery = $searchQuery; 
      } 
    } 

    public function setNumberOfNews ($numberOfNews) { 
      if(!empty($numberOfNews)) { 
        $this->numberOfNews = (int) $numberOfNews; 
      } 
    } 

    public function getNews() { 
      return $this->processNews(); 
    } 

    private function processNews() { 
      $loadXml = simplexml_load_file(urlencode('http://news.google.com/news?q='.$this->searchQuery.'&num='.$this->numberOfNews.'&output=rss')); 

      $news = array(); 

      $i = 0; 
      foreach ($loadXml->channel->item as $item) 
      { 
       preg_match('@src="([^"]+)"@', $item->description, $match); 
       $newsSections = explode('</pre> 
', $item->description); 

$news[$i]['title'] = (string) $item->title; 
$news[$i]['image'] = $match[1]; 
$news[$i]['link'] = (string) $item->link; 
$news[$i]['news_source'] = strip_tags($newsSections[1]); 
$news[$i]['short_story'] = strip_tags($newsSections[2]); 

$i++; 
} 

$result = array('news' => $news, 'status' => '200', 'message' => 'OK'); 

return json_encode($result); 
} 

} 
?> 

這個類解析和轉換RSS飼料輸出到json格式,你可以在Nuhil personal website找到整個帖子,祝你好運。

注: 可能在這個例子中的一些響應輸出領域不存在了,所以你必須檢查的響應項目的格式,並因此調整你的類

+0

android不支持php – Ayush

+0

我知道,這就是爲什麼我建議你嘗試將這個類轉換爲java類 –

相關問題