Java 使用搜索API搜索Google时遇到问题

Java 使用搜索API搜索Google时遇到问题,java,web-services,rest,google-search-api,Java,Web Services,Rest,Google Search Api,我正在尝试使用restful客户端使用google自定义搜索。我基本上是跟着林克走的。我的代码在下面 public class GoogleCrawler { final static String apiKey = "key"; final static String customSearchEngineKey = "CX value"; final static String searchURL = "https://www.googleapis.com/cus

我正在尝试使用restful客户端使用google自定义搜索。我基本上是跟着林克走的。我的代码在下面

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)
如果您知道我做错了什么,我们将不胜感激。

请尝试直接在您的浏览器中执行此呼叫“”

我执行了它并得到:

{
  "error": {
    "errors": [
      {
        "domain": "usageLimits",
        "reason": "keyInvalid",
        "message": "Bad Request"
      }
    ],
    "code": 400,
    "message": "Bad Request"
  }
}
这是一个简单的JSON,因此可以在应用程序中解析它

更新: 看看java中的错误

更新2:

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

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;
}