0

first i have two arrays ArrayList of type JSONObject jsonArrayResponse, jsonArraySubResponse

note: both arrays have the same size

here is my code to get and insert place data into an array :

        for (int i = 0; i<jsonArrayResponse.size() - 1; i++) {

            try {

                JSONObject objectDictionary = jsonArrayResponse.get(i);
                JSONObject objectSubDictionary = jsonArraySubResponse.get(i);

                PlaceObject placeObject = new PlaceObject();

                placeObject.setPlace_id(objectDictionary.getString("place_id"));
                placeObject.setLat(objectDictionary.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
                placeObject.setLng(objectDictionary.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
                placeObject.setName(objectDictionary.getString("name"));

                if (objectDictionary.has("photos")) {

                    JSONObject photoReferenceObject = objectDictionary.getJSONArray("photos").getJSONObject(0);

                    if (photoReferenceObject.has("photo_reference")) {
                        placeObject.setPhotoReference(photoReferenceObject.getString("photo_reference"));
                    }
                }

                if (objectSubDictionary.has("vicinity")) {
                    placeObject.setVicinity(objectSubDictionary.getString("vicinity"));
                }

                if (objectSubDictionary.has("formatted_address")) {
                    placeObject.setFormatted_address(objectSubDictionary.getString("formatted_address"));
                }

                if (objectSubDictionary.has("formatted_phone_number")) {
                    placeObject.setFormatted_phone_number(objectSubDictionary.getString("formatted_phone_number"));
                }

                if (objectSubDictionary.has("international_phone_number")) {
                    placeObject.setInternational_phone_number(objectSubDictionary.getString("international_phone_number"));
                }

                if (objectSubDictionary.has("url")) {
                    placeObject.setUrl(objectSubDictionary.getString("url"));
                }

                if (objectSubDictionary.has("website")) {
                    placeObject.setWebsite(objectSubDictionary.getString("website"));
                }

                if (objectSubDictionary.has("reviews")) {

                    ArrayList<Reviews> reviews = new ArrayList<Reviews>();

                    for (int j = 0; j<objectSubDictionary.getJSONArray("reviews").length(); j++) {

                        Reviews reviewObject = new Reviews();
                        JSONObject review = objectSubDictionary.getJSONArray("reviews").getJSONObject(j);

                        reviewObject.setAuthor_name(review.getString("author_name"));
                        if (review.has("rating")) {
                            reviewObject.setRating(review.getString("rating"));
                        }
                        reviewObject.setText(review.getString("text"));
                        if (review.has("type")) {
                            reviewObject.setType(review.getString("type"));
                        }

                        reviews.add(reviewObject);
                    }

                    placeObject.setReviews(reviews);
                }

                if (objectDictionary.has("opening_hours")) {

                    ArrayList<String> days = new ArrayList<String>();

                    for (int z = 0; z<objectDictionary.getJSONObject("opening_hours").getJSONArray("reviews").length(); z++) {

                        String day = objectDictionary.getJSONObject("opening_hours").getJSONArray("reviews").getString(z);
                        days.add(day);
                    }

                    placeObject.setWeekday_text(days);
                }

                if (objectDictionary.has("opening_hours")) {
                    if (objectDictionary.getJSONObject("opening_hours").has("open_now")) {

                        placeObject.setOpen_now(objectDictionary.getJSONObject("opening_hours").getBoolean("open_now"));
                    }
                }

                Float distanceInKilometers = distance(objectDictionary.getJSONObject("geometry").getJSONObject("location").getDouble("lat"), objectDictionary.getJSONObject("geometry").getJSONObject("location").getDouble("lng"), Global.loc.getLatitude(), Global.loc.getLongitude()) / 1000;

                placeObject.setDistance(distanceInKilometers.doubleValue());

                if (objectDictionary.has("rating")) {
                    placeObject.setRating(objectDictionary.getDouble("rating"));
                }

                if (objectDictionary.has("price_level")) {
                    placeObject.setPrice_level(objectDictionary.getInt("price_level"));
                }

                placeObjects.add(placeObject);

                if (i == jsonArrayResponse.size() - 1) {
                    this.placeObjectsResopones = placeObjects;
                    this.placeObjects = placeObjects;
                }
            } catch (JSONException  e) {
                e.printStackTrace();
            }
        }

Sample Json data:

1) Places Search request:

{
   "html_attributions" : [],
   "results" : [
      {
         "geometry" : {
            "location" : {
               "lat" : -33.870775,
               "lng" : 151.199025
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
         "id" : "21a0b251c9b8392186142c798263e289fe45b4aa",
         "name" : "Rhythmboat Cruises",
         "opening_hours" : {
            "open_now" : true
         },
         "photos" : [
            {
               "height" : 270,
               "html_attributions" : [],
               "photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
               "width" : 519
            }
         ],
         "place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
         "scope" : "GOOGLE",
         "alt_ids" : [
            {
               "place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
               "scope" : "APP"
            }
         ],
         "reference" : "CoQBdQAAAFSiijw5-cAV68xdf2O18pKIZ0seJh03u9h9wk_lEdG-cP1dWvp_QGS4SNCBMk_fB06YRsfMrNkINtPez22p5lRIlj5ty_HmcNwcl6GZXbD2RdXsVfLYlQwnZQcnu7ihkjZp_2gk1-fWXql3GQ8-1BEGwgCxG-eaSnIJIBPuIpihEhAY1WYdxPvOWsPnb2-nGb6QGhTipN0lgaLpQTnkcMeAIEvCsSa0Ww",
         "types" : [ "travel_agency", "restaurant", "food", "establishment" ],
         "vicinity" : "Pyrmont Bay Wharf Darling Dr, Sydney"
      },...
   ],
   "status" : "OK"
}

2) Place Details request:

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "48",
            "short_name" : "48",
            "types" : [ "street_number" ]
         },
         {
            "long_name" : "Pirrama Road",
            "short_name" : "Pirrama Road",
            "types" : [ "route" ]
         },
         {
            "long_name" : "Pyrmont",
            "short_name" : "Pyrmont",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "NSW",
            "short_name" : "NSW",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "AU",
            "short_name" : "AU",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "2009",
            "short_name" : "2009",
            "types" : [ "postal_code" ]
         }
      ],
      "formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
      "formatted_phone_number" : "(02) 9374 4000",
      "geometry" : {
         "location" : {
           "lat" : -33.8669710,
           "lng" : 151.1958750
         },
         "viewport" : {
            "northeast" : {
               "lat" : -33.8665053,
               "lng" : 151.1960371
            },
            "southwest" : {
               "lat" : -33.8669293,
               "lng" : 151.1952183
            }
         }
      },
      "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
      "id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
      "international_phone_number" : "+61 2 9374 4000",
      "name" : "Google Sydney",
      "place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
      "scope" : "GOOGLE",
      "alt_ids" : [
         {
            "place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
            "scope" : "APP"
         }
      ],
      "rating" : 4.70,
      "reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
      "reviews" : [
         {
            "aspects" : [
               {
                  "rating" : 3,
                  "type" : "quality"
               }
            ],
            "author_name" : "Simon Bengtsson",
            "author_url" : "https://plus.google.com/104675092887960962573",
            "language" : "en",
            "rating" : 5,
            "text" : "Just went inside to have a look at Google. Amazing.",
            "time" : 1338440552869
         },
         {
           "aspects" : [
              {
                 "rating" : 3,
                 "type" : "quality"
              }
             ],
            "author_name" : "Felix Rauch Valenti",
            "author_url" : "https://plus.google.com/103291556674373289857",
            "language" : "en",
            "rating" : 5,
            "text" : "Best place to work :-)",
            "time" : 1338411244325
         },
         {
           "aspects" : [
              {
                 "rating" : 3,
                 "type" : "quality"
              }
             ],
            "author_name" : "Chris",
            "language" : "en",
            "rating" : 5,
            "text" : "Great place to work, always lots of free food!",
            "time" : 1330467089039
         }
      ],
      "types" : [ "establishment" ],
      "url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
      "vicinity" : "48 Pirrama Road, Pyrmont",
      "website" : "http://www.google.com.au/"
   },
   "status" : "OK"
}

my problem is i get messing data and i think it skips if data took time to process. how to improve it to make sure i get all data of a place object ?

2 Answers 2

1

I note that

inside your loop this condition will not happen

if (i == jsonArrayResponse.size() - 1) {
 ...
}

because your loop will not continue till i == jsonArrayResponse.size() - 1

try to add = in the loop line like this

for (int i = 0; i <= jsonArrayResponse.size() - 1; i++) { ...}

Maybe this is the reason of missing last element from the array

Sign up to request clarification or add additional context in comments.

3 Comments

i am getting only one object saved at index 0
do u know how to run this operation in background then have completion handler to have an array with all objects in it?
to run this operation in background you can use AsyncTask, Here's good article about run task in background, androidresearch.wordpress.com/2012/03/17/…
0

Please provide sample data. I will try using GSON.

3 Comments

i've added sample json data
From the data It looks like you need to join these 2 jsons. May be data is not sorted. So try to join it based on the geometry long and lat from the search details to place details.
i am already getting the place details by getting place id from places search which means both requests have data but instering them into PlaceObject with array cuases messing data in PlaceObject

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.