1
EDIT 2谷歌自定義搜索引擎機器人不接受20%或空格
發現我的錯誤,我是路過無效無效參數只是remebered我是tryiong出來的東西。
對不起,麻煩GUYS
我使用谷歌自定義搜索引擎的第一次和到目前爲止一切都很好。然而,當我試圖發送一個查詢,其中有一個白色的空間,搜索引擎會返回一個錯誤的請求響應例如
myUrl = (CustomSearchEngineURL + API_KEY + "&cx=" + cxKey + "&q="
+ q.replace(" ", "%20") + "&searchType=" + searchType
+ "&imgType=" + imgType + "&imgSize=" + imgSize + "&num=20&alt=json");
這將返回該
com.google.api.client.http.HttpResponseException: 400 Bad Request
編輯項目
我採取了323go的建議,並嘗試編碼我的q,這是我如何實現它
String encodedParms = null;
try {
encodedParms = URLEncoder.encode(q, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
myUrl = (CustomSearchEngineURL + API_KEY + "&cx=" + cxKey + "&q="
+ encodedParms + "&searchType=" + searchType + "&imgType="
+ imgType + "&imgSize=" + imgSize + "&num=20&alt=json");
Log.d(Tag, myUrl);
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request;
try {
request = httpRequestFactory.buildGetRequest(new GenericUrl(myUrl));
String response = streamToString(request.execute().getContent());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在我的日誌我得到這個作爲URL
https://www.googleapis.com/customsearch/v1?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-w&cx=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&q=Sunway+Lagoon+Theme+Park&searchType=image&imgType=photo&imgSize=xxlarge&num=20&alt=json
我仍然得到了同樣的錯誤請求錯誤
請誰能告訴我我在做什麼撥錯
向我們展示如何調用url。但通常,您應該使用'UrlEncoder'而不是'String.replace'。 – 323go 2013-05-10 02:55:20