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

この結果を表示するきれいなリストを作成しようとしています。これには、Web サイトとその他の情報のみが表示されます。

Jsoup を使用することを考えていましたが、2 つを統合する方法がわかりませんでした。何かアドバイス?

ありがとう、

リチャード。

4

1 に答える 1

0

リチャード、あなたの 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 に答える