How to get opening_hours using the Google Places API for Android
Asked Answered
M

1

2

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

Myrwyn answered 8/3, 2017 at 16:3 Comment(2)
#20735516Anniceannie
@MCZ that's not an answer, that is for javascript, ! But I was asking if I can work with Places API Web Service in the client side (Android) !? Or there is a way using the api for Android!Myrwyn
O
2

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/

Octosyllabic answered 11/3, 2017 at 4:21 Comment(5)
Sorry for the delay response, did you used this api? because I'm not able to use it in the Android Client, because I'm getting invalid key.Myrwyn
It work perfectly .. this is live code in my projectOctosyllabic
This url work in browser or not check it first ? if any error please tell meOctosyllabic
Harsil, I was having problems with the url and the api key, now its works, but I'm not sure this is ok because of "If you're building a client-side app, check out the Google Places API for Android and Site Library in Google Maps JavaScript API." what do you think? Thanks you very muchl!!Myrwyn
i use "Google Places API Web Service" in Android and iOS both. i know there is other option "Google Places Android API" also after some year ago there is only one option Web Service API so i use this . and second problem is some country Google Places iOS sdk give wrong suggestion and Web API work perfect .Octosyllabic

© 2022 - 2024 — McMap. All rights reserved.