有些地点根本没有这个字段。这对他们来说在逻辑上也是必需的,也没有在该API的数据存储中记录小时。
您的代码应该是这样的:
String uriPath = "https://maps.googleapis.com/maps/api/place/details/json";
String uriParams = "?placeid=" + currentPlaceID +
"&key=" + GOOGLE_MAPS_WEB_API_KEY;
String uriString = uriPath + uriParams;
// Using Volley library for networking.
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JSONObject response = null;
// Required for the following JsonObjectRequest, but not really used here.
Map<String, String> jsonParams = new HashMap<String, String>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
uriString,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if (response != null) {
// Retrieve the result (main contents).
JSONObject result =
response.getJSONObject("result");
// Acquire the hours of operation.
try {
JSONObject openingHoursJSON =
result.getJSONObject("opening_hours");
// Determine whether this location
// is currently open.
boolean openNow =
openingHoursJSON.getBoolean("open_now");
// Record this information somewhere, like this.
myObject.setOpenNow(openNow);
} catch (JSONException e) {
// This `Place` has no associated
// hours of operation.
// NOTE: to record uncertainty in the open status,
// the variable being set here should be a Boolean
// (not a boolean) to record it this way.
myObject.setOpenNow(null);
}
}
// There was no response from the server (response == null).
} catch (JSONException e) {
// This should only happen if assumptions about the returned
// JSON structure are invalid.
e.printStackTrace();
}
} // end of onResponse()
}, // end of Response.Listener<JSONObject>()
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(LOG_TAG, "Error occurred ", error);
}
}); // end of new JsonObjectRequest(...)
// Add the request to the Volley request queue.
// VolleyRequestQueue is a singleton containing a Volley RequestQueue.
VolleyRequestQueue.getInstance(mActivity).addToRequestQueue(request);
这占了开放时间不是用于当前天的可能性。清楚的是,这是一个异步操作。它可以做成同步的,但这超出了这个答案的范围(并且通常优选异步)。