0

我一直在使用 GSON 和 JSON 来完成搜索并显示结果。我有这段代码,但我无法让它显示结果:

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

}

更新:

我可以使用搜索得到一些结果,它显示在列表中:

<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]]

我正在尝试创建一个干净的列表,该列表将显示这个结果,只显示网站,也许还有更多信息。

我正在考虑使用 Jsoup,但不确定如何将两者结合起来。有什么建议吗?

谢谢,

理查德。

4

1 回答 1

0

Richard,假设您的 GoogleResults 类是一个与响应中的属性匹配的 java bean,您只需要正确编码您的搜索查询:

改变:

URL url = new URL(google + search);

到:

URL url = new URL(google + URLEncoder.encode(search,"UTF-8"));
于 2012-10-31T17:05:18.953 回答