2012-06-26 36 views
2

我有一個小的Java應用程序,給定一個谷歌地方的參考列表,必須得到每個所述的谷歌地方的Id(長話短說,我們存儲參考的地方而不是他們的Id,並且現在才意識到每個地方的引用不是唯一的)。地方詳細信息請求失敗 - 引用太長

我的應用程序完美適用於列表中約95%的地方,但對於某些記錄而言,失敗時會顯示「NOT_FOUND」狀態代碼。一些調查顯示,這些特定地點的地點參考是(當與前綴https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key=myApiKey結合使用時)對於URL約2個字符太長。最後幾個字符被截斷。

我最初的想法是,我只想做一個POST請求,谷歌的地方API,但發送相同的到的POST請求當我取回從谷歌服務器「REQUEST_DENIED」狀態碼。

反正有沒有,或者這只是一個與谷歌地方API(現在的地方數已推動參考太長?)的緊急錯誤?)。

我還應該注意,失敗的地方最近都是由我們的應用程序添加的。

這就是我目前(工作95%)的代碼如下所示:

public static JSONObject getPlaceInfo(String reference) throws Exception 
{ 
URL places = new URL("https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key="+apiKey+"&reference="+reference); 
    URLConnection con = places.openConnection(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    StringBuffer input = new StringBuffer(); 
    String inputLine; 
    while ((inputLine = in.readLine()) != null) 
     input.append(inputLine); 
    in.close(); 

    JSONObject response = (JSONObject) JSONSerializer.toJSON(input.toString()); 
    return response; 
} 

,這是我的 「ACCESS_DENIED」 後的代碼是什麼樣子:

public static JSONObject getPlaceInfo(String reference) throws Exception 
{ 
    String data = URLEncoder.encode("sensor", "UTF-8") + "=" + URLEncoder.encode("true", "UTF-8"); 
    data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(apiKey, "UTF-8"); 
    data += "&" + URLEncoder.encode("reference", "UTF-8") + "=" + URLEncoder.encode(reference, "UTF-8"); 

    URL places = new URL("https://maps.googleapis.com/maps/api/place/details/json"); 
    URLConnection con = places.openConnection(); 

    con.setDoOutput(true); 
    OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); 
    wr.write(data); 
    wr.flush(); 

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
    StringBuffer input = new StringBuffer(); 
    String inputLine; 
    while ((inputLine = in.readLine()) != null) 
     input.append(inputLine); 
    in.close(); 

    JSONObject response = (JSONObject) JSONSerializer.toJSON(input.toString()); 
    return response; 
} 

的一個例子參考失敗是:提前

CnRtAAAAxm0DftH1c5c6-krpWWZTT51uf0rDqCK4jikWV6eGfXlmKxrlsdrhFBOCgWOqChc1Au37inhf8HzjEbRdpMGghYy3dxGt17FEb8ys2CZCLHyC--7Vf1jn-Yn1kfZfzxznTJAbIEg6422q1kRbh0nl1hIQ71tmdOVvhdTfY_LOdbEoahoUnP0SAoOFNkk_KBIvTW30btEwkZs 

謝謝!

回答

0

您正在發送您的請求參數,但API不支持。有一個關於GET和請求參數在一個很好的答案:

HTTP GET with request body

下面的代碼應該將細節請求工作:

private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place"; 
private static final String TYPE_DETAILS = "/details"; 
private static final String OUT_JSON = "/json"; 

HttpURLConnection conn = null; 
StringBuilder jsonResults = new StringBuilder(); 
try { 
    StringBuilder sb = new StringBuilder(PLACES_API_BASE); 
    sb.append(TYPE_DETAILS); 
    sb.append(OUT_JSON); 
    sb.append("?sensor=false"); 
    sb.append("&key=" + API_KEY); 
    sb.append("&reference=" + URLEncoder.encode(reference, "utf8")); 

    URL url = new URL(sb.toString()); 
    conn = (HttpURLConnection) url.openConnection(); 
    InputStreamReader in = new InputStreamReader(conn.getInputStream()); 

    // Load the results into a StringBuilder 
    int read; 
    char[] buff = new char[1024]; 
    while ((read = in.read(buff)) != -1) { 
     jsonResults.append(buff, 0, read); 
    } 
} catch (MalformedURLException e) { 
    return null; 
} catch (IOException e) { 
    return null; 
} finally { 
    if (conn != null) { 
     conn.disconnect(); 
    } 
} 

try { 
    // Create a JSON object hierarchy from the results 
    JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result"); 
    jsonObj.getString("name"); 
} catch (JSONException e) { 
    Log.e(LOG_TAG, "Error processing JSON results", e); 
} 
+0

這確實解決了ACCESS_DENIED消息(並解釋爲什麼它沒有工作,如果我的代表足夠高,我會+1!),但它與我的第一個(95%工作)代碼片段差不多。由於巨大的參考參數,這並不能解決GET url過長的問題。 – Lawrence

相關問題