2012-06-26 2 views
2

Google 장소에 대한 참조 목록이 제공되는 작은 Java 응용 프로그램을 사용하여 해당 Google 장소 각각에 대한 ID를 가져와야합니다 (긴 이야기만으로도 장소에 대한 참조를 저장하고있었습니다. ID 대신에 참조가 장소마다 고유하지 않음을 깨달았습니다).장소 세부 정보 요청 실패 - 참조 길이가 너무 깁니다.

내 앱은 목록의 장소 중 약 95 %에서 완벽하게 작동하지만 일부 레코드는 "NOT_FOUND"상태 코드로 실패합니다. 일부 조사 결과에 따르면 이러한 특정 장소에 대한 장소 참조는 (URL 앞에 https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key=myApiKey와 결합 된 경우) 약 2자를 URL에 대해 너무 길게 나타냅니다. 마지막 두 자 수가 잘 렸습니다.

내 초기 생각은 내가 Google 장소 API에 대한 POST 요청을 할 것이라고했지만 POST 요청을 보낼 때 Google 서버에서 "REQUEST_DENIED"상태 코드를 다시받습니다.

어쨌든이 문제가 발생 했습니까? 아니면 Google 작업 공간 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 및 요청 PARAMS에 대한 좋은 답이있다 :

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

관련 문제