Java 使用Google'获取更多信息;斯格森
我有一个我在上面找到的程序,允许用户输入查询并使用谷歌搜索输入的任何内容。然后程序返回Google给出的前4个结果。代码如下:Java 使用Google'获取更多信息;斯格森,java,ajax,json,gson,Java,Ajax,Json,Gson,我有一个我在上面找到的程序,允许用户输入查询并使用谷歌搜索输入的任何内容。然后程序返回Google给出的前4个结果。代码如下: public class TestGoogleSea { public static void main(String[] args) throws IOException { String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
public class TestGoogleSea {
public static void main(String[] args) throws IOException {
String address = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String query = "who was the 19th president of america";
String charset = "UTF-8";
URL url = new URL(address + URLEncoder.encode(query, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
int total = results.getResponseData().getResults().size();
System.out.println("total: "+total);
// Show title and URL of each results
for(int i=0; i<=total-1; i++){
System.out.println("Title: " + results.getResponseData().getResults().get(I).getTitle());
System.out.println("URL: " + results.getResponseData().getResults().get(I).getUrl() + "\n");
}
}
}
class GoogleResults{
private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }
static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}
static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Result[url:" + url +", title:" + title + "]"; }
}
}
公共类TestGoogleSea{
公共静态void main(字符串[]args)引发IOException{
字符串地址=”http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String query=“谁是美国第19任总统”;
字符串charset=“UTF-8”;
URL=新的URL(地址+URLEncoder.encode(查询,字符集));
Reader Reader=新的InputStreamReader(url.openStream(),字符集);
GoogleResults=new Gson().fromJson(reader,GoogleResults.class);
int total=results.getResponseData().getResults().size();
系统输出打印项次(“总计:+总计);
//显示每个结果的标题和URL
对于(int i=0;iStack代码段目前仅支持JavaScript、HTML和CSS。抱歉,我这么做的唯一原因是为了让您能够以IDE中的方式查看代码。这不是问题;我只是让您知道,以供将来参考(我不确定为什么您甚至可以将代码段放在未标记其支持的语言的帖子中).我想你的意思是谷歌的知识图。谷歌使用。具体来说,我认为这是你想要的。我试过了,但找不到导入com.jayway.jsonpath.jsonpath的.jar文件;
Total: 4
Title: Rutherford B. Hayes - Wikipedia, the free encyclopedia
URL: http://en.wikipedia.org/wiki/Rutherford_B._Hayes
Title: List of <b>Presidents</b> of the United States - Wikipedia, the free <b>...</b>
URL: http://en.wikipedia.org/wiki/List_of_Presidents_of_the_United_States
Title: Rutherford B. Hayes | The White House
URL: http://www.whitehouse.gov/about/presidents/rutherfordbhayes
Title: <b>American President</b>: Rutherford Birchard Hayes - Miller Center
URL: http://millercenter.org/president/hayes