1

JavaでGoogleカスタム検索APIの使用を開始していますが、接続していません。Google から json データを受信して​​います (はずですが、接続していません..:()

 HttpResponse response = httpClient.execute(httpGet); 
 // exception is thrown at this point.

これは私のコードの行です。コードは次のとおりです。

public class MyGoogleSearch{
final static  String searchURL = "https://www.googleapis.com/customsearch/v1?";
// This is Important : 

final static String apiKey = "My-Key-has a '-'";
final static String customSearchEngineKey = "My-Custom-search-engine-that-has a ':' too";

public String makeSearchString(String qSearch){
    String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey;
    toSearch += "&q=" + qSearch + "&alt=json";
    return toSearch;
}

public static void main(String[] argv) throws IOException{
    System.setProperty("proxyHost", "my-host");
    System.setProperty("proxyPort","my-Port");

    MyGoogleSearch browser = new MyGoogleSearch();
    String toSearch = browser.makeSearchString("flower");
    System.out.println(toSearch);
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(toSearch);

    HttpResponse response = httpClient.execute(httpGet);
    // exception is thrown here.
    HttpEntity entity = response.getEntity();

    if(entity != null){
        InputStream inStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
        int length = 0;
        byte[] buffer  = new byte[2048];
        while((length = inStream.read(buffer)) != -1)
            System.out.println(buffer);
        String inputLine;
        while((inputLine = reader.readLine()) != null)
            System.out.println(inputLine);
    }
}

}

// エラーは次のとおりです。

Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connection to https://www.googleapis.com refused at   
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)

....

Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)

これは有効な方法ですか?UTF-8 エンコーディングは一切行っていません。それは問題でしょうか?

このページで:

http://code.google.com/apis/customsearch/v1/getting_started.html
JSON/Atom カスタム検索 API でこれがどのように機能するかの例を次に示します。この API は、テスト カスタム検索エンジンで講義を検索します。

https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=講義を取得

// これは javaScript 専用か、それとも Java 用か

ただし、カスタム検索エンジンのキーをcxに配置しましたが、これは正しいはずです

Google api Explorer に行きましたhttps://code.google.com/apis/explorer/

カスタム検索を選択し、プログラムに入力した *q および cx 列を入力しました。

結果が表示され、GET https://www.googleapis.com/customsearch/v1?q=o&cx= {MY-Custom-Search-Engine-No}&pp=1&key={YOUR_API_KEY}

しかし、今回は cx 値の ':' 文字の場所に %3 がありました。

私のキーとcse-noのすべての文字に対してどうすればよいですか

4

1 に答える 1

0

問題はgoogleapisへの接続にあるようですが、正しいプロキシを使用していることを確認しますか?google.comまたは任意のURLに接続して、プロキシを通過していることをテストしてみてください。プロキシを設定せずに試してください。

于 2011-09-04T20:58:31.953 に答える