简体   繁体   English

显示搜索结果

[英]Displaying results from search

I have been playing with GSON and JSON to complete a search and display the results. 我一直在使用GSON和JSON来完成搜索并显示结果。 I have this piece of code but I can't get it to display the results: 我有这段代码,但无法显示结果:

public static void main(String args[]) throws IOException
{

    String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
    String search = "food pantries in Dallas";
    String charset = "UTF-8";

    URL url = new URL(google + URLEncoder.encode(search, charset));
    Reader reader = new InputStreamReader(url.openStream());
    GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);


    // Show title and URL of 1st result.
    System.out.println(results.getResponseData().getResults().get(0).getTitle());
    System.out.println(results.getResponseData().getResults().get(0).getUrl());
    System.out.println(results.getResponseData().getResults());

}

UPDATE: 更新:

I am able to get some results using the search and it is displayed list this: 我可以使用搜索获得一些结果,并显示以下列表:

<b>Food Pantries</b> | Soup Kitchens | <b>Food Banks</b>
http://www.foodpantries.org/
[Result[url:http://www.foodpantries.org/,title:<b>Food Pantries</b> | Soup Kitchens | <b>Food Banks</b>], Result[url:http://feedingamerica.org/foodbank-results.aspx,title:Find a Local <b>Food Bank</b> | Feeding America], Result[url:http://www.foodbanknyc.org/,title:<b>Food Bank</b> for New York City], Result[url:http://en.wikipedia.org/wiki/Food_bank,title:<b>Food bank</b> - Wikipedia, the free encyclopedia]]

I am trying to create clean list that will display this results showing just the Websites and maybe some more information. 我正在尝试创建一个干净的列表,以显示此结果,仅显示网站以及更多信息。

I was thinking about using Jsoup but was not sure how to integrate the two. 我当时正在考虑使用Jsoup,但不确定如何将两者集成在一起。 Any advice? 有什么建议吗?

Thanks, 谢谢,

Richard. 理查德

Richard, assuming your GoogleResults class is a java bean that matches the attributes in the response, you just need to properly encode your search query: 理查德(Richard),假设您的GoogleResults类是一个与响应中的属性匹配的Java Bean,您只需要正确编码搜索查询即可:

Change: 更改:

URL url = new URL(google + search);

to: 至:

URL url = new URL(google + URLEncoder.encode(search,"UTF-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM