Single json Api Solution for two relative Json APIs

291 views Asked by At

I am using two Json Apis for my application, NearbyPlaces API returns me place "name " & "vicinity" where as Google Distance Matrix API returns me "duration" in minutes .

After getting datas from json Api I am trying to load the datas to RecyclerView using Volley library .I am able to parse the response of nearbyplace and able to load "name" and "vicinity" in RecyclerView but getting problem in loading "duration" from current location.

I am writting following codes to parse nearbyPlaces inside volley response :

  String NEARBY_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+current_latitude+","+current_longitude+"&radius=500&types=embassy|shopping_mall|school|zoo|taxi_stand&key=AIzaSyD784AbXVmgxPLyeIUG137FEMu-82sadK4";
    Log.e("Response:", NEARBY_URL);
    final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, NEARBY_URL, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.e("Response:", response.toString());
            try {
                String status = response.getString("status");
                if (status.equalsIgnoreCase("OK")) {

                    JSONArray arrayObject = response.getJSONArray("results");
                    Log.e("Array Obj:", arrayObject.toString());


                    name_array = new ArrayList<>();
                    add_array = new ArrayList<>();
                    latitide_array=new ArrayList<>();
                    longitude_aray =new ArrayList<>();


                    for (int i = 0; i < arrayObject.length(); i++) {

                        JSONObject c = arrayObject.getJSONObject(i);
                        //below codes for current place and vicinity
                        String name = c.getString("name");
                        String address = c.getString("vicinity");
                        Log.e("Name", name);
                        Log.e("Address", address);
                        name_array.add(name);
                        add_array.add(address);
                        //below code to get latitude and longitude of specific location
                        JSONObject geometry = c.getJSONObject("geometry");
                        JSONObject locaction = geometry.getJSONObject("location");
                        Double lat = locaction.getDouble("lat");
                        Double lng = locaction.getDouble("lng");
                        Log.e("Latitude>>>>", lat.toString() + " " + lng.toString());
                        latitide_array.add(lat);
                        longitude_aray.add(lng);


                        //adding places detail in ArrayList for passing a pararmeter for RecyclerAdapter
                        nearby_items.add(new NearbyPlaces(name_array.get(i).toString(), add_array.get(i).toString(), "0.2", R.drawable.icon_rate));
                    }


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Error: ", "Error: " + error.getMessage());
            Toast.makeText(getActivity(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hide the progress dialog
            // hidepDialog();
        }
    });

    // Adding request to request queue
    app.addToRequestQueue(jsonObjReq);
}

The problem I am facing is I am not being able to parse "Googel Distance Matrix API" with in same Response inside volley.

Is it possible to parse more than single API from Volley? or we can use single API to get "place name","address" and"distance between two GeoPoints " I googled it too but didn't find any solution,expecting some guide lines here ,Thank you

0

There are 0 answers