【问题标题】:Place detail request failing - reference too long地方详细信息请求失败 - 参考太长
【发布时间】:2012-06-26 12:53:49
【问题描述】:

我有一个小型 java 应用程序,给定 google 地点的引用列表,必须取回每个所说的 google 地点的 ID(长话短说,我们存储地点的引用而不是他们的 ID,直到现在意识到每个地方的引用不是唯一的)。

我的应用程序在列表中大约 95% 的地方都能完美运行,但某些记录的状态代码为“NOT_FOUND”而失败。一些调查显示,这些特定地点的地点引用(与https://maps.googleapis.com/maps/api/place/details/json?sensor=false&key=myApiKey 前缀结合使用时)对于 URL 来说大约有 2 个字符太长。最后几个字符被截断。

我最初的想法是我只是向 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

提前致谢!

【问题讨论】:

    标签: java google-maps google-places-api


    【解决方案1】:

    您在正文中发送请求参数,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);
    }
    

    【讨论】:

    • 这确实解决了 ACCESS_DENIED 消息(并解释了为什么它不起作用,如果我的代表足够高,我会 +1!),但它或多或少与我的第一个相同(95% 工作) 代码 sn-p。这并不能解决由于引用参数过大导致 GET url 过长的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多