无法使用搜索API搜索Google

时间:2014-02-28 02:15:25

标签: java web-services rest google-search-api

我正在尝试使用restful客户端进行Google自定义搜索。我基本上遵循this链接。我的代码在

之下
public class GoogleCrawler {

    final static String apiKey = "key";
    final static String customSearchEngineKey = "CX value";

    final static  String searchURL = "https://www.googleapis.com/customsearch/v1?";

    private static String makeSearchString(String qSearch,int start,int numOfResults)
    {
        String toSearch = searchURL + "key=" + apiKey + "&cx=" + customSearchEngineKey+"&q=";

        //replace spaces in the search query with +
        String keys[]=qSearch.split("[ ]+");
        for(String key:keys)
        {   
            toSearch += key +"+"; //append the keywords to the url
       }        

            //specify response format as json
            toSearch+="&alt=json";

            //specify starting result number
            toSearch+="&start="+start;

            //specify the number of results you need from the starting position
            toSearch+="&num="+numOfResults;

        return toSearch;
    }

    private static String read(String pUrl)
    {
        //pUrl is the URL we created in previous step
        try
       {
             URL url=new URL(pUrl);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            BufferedReader br=new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuffer buffer=new StringBuffer();
            while((line=br.readLine())!=null){
                buffer.append(line);
            }
            return buffer.toString();
        }catch(Exception e){
            e.printStackTrace();
       }
        return null;
    }

    public static void main (String[] args)
    {

        String toSearch=makeSearchString("katrina",1,100);
        System.out.println(read(toSearch));
    }
}

我从服务器收到错误的HTTP 400请求。

java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.googleapis.com/customsearch/v1?key=key&cx=cx&q=katrina+&alt=json&start=1&num=100
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at com.main.GoogleCrawler.read(GoogleCrawler.java:45)
    at com.main.GoogleCrawler.main(GoogleCrawler.java:62)

任何关于我做错事的想法都会受到赞赏

1 个答案:

答案 0 :(得分:1)

尝试直接在浏览器中调用“https://www.googleapis.com/customsearch/v1?key=key&cx=cx&q=katrina+&alt=json&start=1&num=100”。

我执行了它并得到了:

{
  "error": {
    "errors": [
      {
        "domain": "usageLimits",
        "reason": "keyInvalid",
        "message": "Bad Request"
      }
    ],
    "code": 400,
    "message": "Bad Request"
  }
}

这是一个简单的JSON,因此可以在您的应用程序中进行解析。

更新:  查看How to find out specifics of 400 Http error in Java? error-in-java

UPDATE2:

您可以重写代码以获得更多信息来解析

private static String read(String pUrl) {
    // pUrl is the URL we created in previous step
    try {
        URL url = new URL(pUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        int statusCode = connection.getResponseCode();
        InputStream is;
        if (statusCode != HttpURLConnection.HTTP_OK) {
            is = connection.getErrorStream();
        } else {
            is = connection.getInputStream();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuilder buffer = new StringBuilder();
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}