放置详细信息请求失败

我有一个小的java应用程序,给定一个谷歌地方的参考列表,必须取回每个所述谷歌地点的Id(长话短说,我们存储的地方,而不是他们的ID的引用,只有现在才意识到,引用每个地方都不是唯一的)。

我的应用程序完美适用于列表中大约95%的位置,但对于某些记录而言失败时会显示“NOT_FOUND”状态码。 某些调查显示,这些特定地点的地点参考(如果与https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key=myApiKey前缀结合使用)大约2个字符太长一个URL。 最后几个字符被截断。

我最初的想法是,我只会向Google Places API发出一个POST请求,但是在将它发送到POST请求时,我会从Google服务器获取“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

提前致谢!


您正在将正文中的请求参数发送出去,而API不支持这些参数。 有关GET的一个很好的答案,并请求params:

带请求正文的HTTP GET

以下代码适用于放置详细信息请求:

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);
}
链接地址: http://www.djcxy.com/p/63159.html

上一篇: Place detail request failing

下一篇: What is the `sensor` parameter in Google Places API good for?