【问题标题】:Trouble searching Google using the search API使用搜索 API 搜索 Google 时遇到问题
【发布时间】:2014-02-28 02:15:24
【问题描述】:

我正在尝试使用 google 自定义搜索并使用 restful 客户端。我基本上关注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)

任何关于我做错了什么的想法将不胜感激

【问题讨论】:

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


    【解决方案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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 2016-12-07
      相关资源
      最近更新 更多