【发布时间】:2020-05-03 14:52:53
【问题描述】:
这是我在提出请求后从 PlacesApi 得到的响应。 但问题是我无法获得“photo_reference”的值。 问题也是 Objects are in Arrays and all ,整个响应看起来令人困惑。
以下是我在android studio Java中尝试过的
private void getUserLocationImage(String mLocationName) {
String url = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input="+mLocationName+"&inputtype=textquery&fields=photos&key="+R.string.google_api;
// prepare the Activities Request
JsonObjectRequest getWeatherRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray dataArray = response.getJSONArray("candidates");
JSONObject photosObj = dataArray.getJSONObject(0);
JSONArray photosArray = photosObj.getJSONArray("photos");
JSONObject photoRefObj = photosArray.getJSONObject(0);
String imageRef = photoRefObj.get("photo_reference").toString();
Toast.makeText(HomeLandingPageActivity.this, ""+imageRef, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
});
// add it to the RequestQueue
queue.add(getWeatherRequest);
}
这是错误
org.json.JSONException: Index 0 out of range [0..0)
这就是网络中的响应
{
"candidates" : [
{
"photos" : [
{
"height" : 4160,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111684034547030396888\"\u003eCaroline Wood\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAQkMptoZgWJHING5qIR5_abXvnxjhHHEOHmDRH3ZpXUrar5PfpN5tQhhPoPwYmTDjpdVmXeT3T9klnrdK4xMvuudPm309UxMcx_ddbiu6E4shWYaPFn4gO4Diq4mOM46EEhCoo3TLpUbrWhInjelgVtYZGhSDJPyoRefWJ8WIcDs8Bk8VXAwHyQ",
"width" : 3120
}
]
}
],
"status" : "OK"
}
【问题讨论】:
-
响应中有什么令人困惑的地方?您以前使用过 JSON 吗?另外,请展示一些您如何尝试处理 JSON 的代码。
-
i cant get the value of "photo_reference"- 你现在使用什么代码来获取这个值?the whole response looks confusing.- 据我所知,这是有效的 JSON。你的意思是字符串被编码成不同的格式? -
@Ajeeli 你能帮我写代码吗?
-
@Nosiku Minyoi,你没有说明问题是什么
-
@Ajeeli 请检查编辑
标签: java json android-studio google-places-api