How to get opening_hours using the Google Places A

2019-07-13 03:07发布

问题:

I'm working with the Google Places API for Android and I want to have access to the opening hours information. I have searched and in the Google Places API Web Service the atribute opening_hours is abailable, but in the documentation you can read:

The Google Places API Web Service should be used in server applications. If you're building a client-side app, check out the Google Places API for Android and Site Library in Google Maps JavaScript API.

Here is object Place - from Google Apis for Android. And here the result of the details of one place - from Places Api W.S.

How can I get the opening hours of one place using the api for Android? Is that possible? May I use the api for W.S even what google says?

Thanks

回答1:

Use this code : read all comment line carefully .

// A class to parse the Google Place Details in JSON format 
    private class ParserTask extends AsyncTask<String, Integer, HashMap<String,String>>{

        JSONObject jObject;
        @Override
        protected HashMap<String,String> doInBackground(String... jsonData) {

            HashMap<String, String> hPlaceDetails = null;
            PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();

            try{
                // this object give JSON
                jObject = new JSONObject(jsonData[0]);

                // this object give Result
                JSONObject jresult = new JSONObject(jObject[1]);            

                // this object give opening_hours
                JSONObject jopening_hours = new JSONObject(jresult[2]);

                // this object give periods
                JSONObject jperiods = new JSONObject(jopening_hours[2]);



            }catch(Exception e){
                Log.d("Exception",e.toString());
            }
            return jperiods;
        }

        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(JsonObject periods){
                HashMap<String,String> periods;

                // put your condition 
                for(..)
                {
                JsonObject day = new JSONObject(periods[i]);
                    periods.put("key",day.get("day"));

                } 
              }
    }

[0] for Monday and [6] for Sunday

JSON look like https://maps.googleapis.com/maps/api/place/details/json placeid=ChIJKfCbbyyNWTkRAPM1frrO_sM&key=YOUR_API_KEY

Beautify json file : http://jsonviewer.stack.hu/