Android应用程序中的Google自定义搜索API

时间:2014-04-22 08:35:05

标签: android json google-custom-search

我一直在尝试使用Google自定义搜索API进行学校工作,并且在尝试获取结果的json格式时遇到了一些问题,我已经在Google开发者控制台和Google自定义上配置了所有内容搜索引擎控制面板,至少我认为。这是我的代码段

String qstring = "key="+key + "&cx="+ cx +"&q="+searchtext+"&alt=json"+"&start="+"0";
    String query = null;
    try {
        query = URLEncoder.encode(qstring, "UTF-8")
        .replaceAll("\\%28", "(")
        .replaceAll("\\%29", ")") 
        .replaceAll("\\+", "%20") 
        .replaceAll("\\%27", "'") 
        .replaceAll("\\%21", "!") 
        .replaceAll("\\%7E", "~");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    URL url = null;
    try {
        url = new URL("https://www.googleapis.com/customsearch/v1?" + query);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpURLConnection conn2 = null;
    try {
        conn2 = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        conn2.setRequestMethod("GET");
    } catch (ProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    conn2.setRequestProperty("Accept", "application/json");
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(
        (conn2.getInputStream())));
        String line;
        while ((line = br.readLine()) != null) {
            content.append(line + "\n");
        }
        br.close();

    return content.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

并且错误显示在logcat

04-22 07:26:36.253: W/System.err(4229): java.io.FileNotFoundException: https://www.googleapis.com/customsearch/v1?key%3DAIzaSyBgvvgYg3mYS66fMM9j0qpaG6wlvUc1KLk%26cx%3D008838294879486691568%3Axy_pvqrl6fa%26q%3Djava.pdf%26alt%3Djson%26start%3D0
04-22 07:26:36.289: W/System.err(4229):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
04-22 07:26:36.321: W/System.err(4229):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)

如果我做错了什么,我会欣赏正确方向的一点。请帮忙!

1 个答案:

答案 0 :(得分:0)

我不确定这是否会对您的情况有所帮助,但我的搜索字符串遇到了很多麻烦,直到我弄清楚如何正确编码它。我注意到你了 进行大量手动插入代码(例如,%20),让对象方法完成工作可能更可靠。我不是在Java工作,但我在这里是怎么做的 在C#中。我分别对APIKey,CSKey和查询进行了编码,并将所有三个添加到基本谷歌搜索字符串中。然后我添加了"& alt = json"到最后。字符串 变量'结果'保存返回的字符串。

string myQuery = "Action Motivation, Inc.";
string final = string.Format("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}",
            HttpUtility.UrlEncode(APIKey),
            HttpUtility.UrlEncode(CXKey),
            HttpUtility.UrlEncode(myQuery));
final += "&alt=json";
WebRequest myRequest = WebRequest.Create(final);
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream myStream = myResponse.GetResponseStream();
StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = myReader.ReadToEnd();