Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何用JSON而不是XML从Google新闻中获得响应_Android - Fatal编程技术网

Android 如何用JSON而不是XML从Google新闻中获得响应

Android 如何用JSON而不是XML从Google新闻中获得响应,android,Android,当我点击时,我想从google新闻中获得json响应,而不是xml响应 我认为您可以轻松地将这个php类转换为java类并使用它: <?php ** * Google-News feed parser and JSON provider Class * * @package None * @author Nuhil Mehdy <nuhil@nuhil.net> */ class GoogleNews { public $searchQuery = 'G

当我点击时,我想从google新闻中获得json响应,而不是xml响应


我认为您可以轻松地将这个php类转换为java类并使用它:

<?php
 **
 * Google-News feed parser and JSON provider Class
 *
 * @package None
 * @author Nuhil Mehdy <nuhil@nuhil.net>
 */
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);
 }
 
}
?>
searchQuery.&num=.$this->numberOfNews.&output=rss');
$news=array();
$i=0;
foreach($loadXml->channel->item as$item)
{
预匹配('@src=“([^”]+)“@',$item->description,$match);
$newsSections=explode('
“,$item->description);
$news[$i]['title']=(字符串)$item->title;
$news[$i]['image']=$match[1];
$news[$i]['link']=(字符串)$item->link;
$news[$i]['news\u source']=strip\u标签($newsSections[1]);
$news[$i]['short_story']=strip_标签($newsSections[2]);
$i++;
}
$result=array('news'=>$news,'status'=>'200','message'=>'OK');
返回json_encode($result);
}
}
?>
这个类解析并转换rss提要输出为json格式,你可以在上面找到整个帖子,祝你好运

注意:
可能本例中的一些响应输出字段已不存在,因此您必须检查响应项的格式并调整您的类,因此

android不支持phpI知道,这就是为什么我建议您尝试将此类转换为java类,为什么不使用?