How to get opening hours of a Place with Place Autocomplete intent builder in Android?

Viewed 696

Currently, I am building a functionality to search for a company with an AutocompleteTextView. To achieve this I use A PlaceAutocomplete IntentBuilder with the overlay mode like this:

   try {
        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ESTABLISHMENT)
                .setCountry("NL")
                .build();
        Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_OVERLAY)
                .setFilter(typeFilter)
                .build(activity);
        activity.startActivityForResult(intent, placeAutocompleteRequestCode);
    } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
        Log.e(TAG, "Something went wrong getting the places fragment." + e.getMessage());
    }

I receive the place like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_AUTOCOMPLETE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            Log.i(TAG, "Place: " + place.getName());
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            Log.i(TAG, status.getStatusMessage());
        } else if (resultCode == RESULT_CANCELED) {
            // The user canceled the operation.
        }
    }
}

But I can't find the opening times in the 'place' object. How am I able to retrieve them? I can only find others who use the API, but I prefer this autocomplete fragment.

Also tried it with an API call to get the place details by place ID later but it appears that this is only allowed for server keys. If someone has this working with a release key on Android then it would be a nice hotfix for now. But still feels cumbersome.

2 Answers

Please use this code below :

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"));

                } 
              }
    }
Related