放置详细信息请求失败 - 参考时间过长

时间:2012-06-26 12:53:50

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

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

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

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

提前致谢!

1 个答案:

答案 0 :(得分: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);
}
相关问题