Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. private void makeRequest(String url,final Location location){
  2. Log.d(TAG, "Url: " + url);
  3. RequestQueue queue = Volley.newRequestQueue(mContext);
  4. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
  5. @Override
  6. public void onResponse(String response) {
  7. try {
  8. JSONObject json = new JSONObject(response);
  9. JSONArray results = json.getJSONArray("results");
  10.  
  11. List<LocationContent> list = new ArrayList<>();
  12. Log.d(TAG, "no of results: " + results.length());
  13. if(results.length() > 0) {
  14. for (int i = 0; i < results.length(); i++) {
  15. JSONObject googlePlace = results.getJSONObject(i);
  16.  
  17. Double distance = HelperClass.getDistanceFromLatLonInKm(
  18. location.getLatitude(),location.getLongitude(),
  19. googlePlace.getJSONObject("geometry").getJSONObject("location").getDouble("lat")
  20. ,googlePlace.getJSONObject("geometry").getJSONObject("location").getDouble("lng")) / 1000;
  21.  
  22. String distanceBetweenTwoPlace = String.valueOf(distance);
  23. String distance_new = distanceBetweenTwoPlace.substring(0, 3);
  24. list.add(new LocationContent(googlePlace.getString("name"),
  25. googlePlace.getJSONObject("geometry").getJSONObject("location").getDouble("lat"),
  26. googlePlace.getJSONObject("geometry").getJSONObject("location").getDouble("lng"),Double.valueOf(distance_new)));
  27. }
  28. }
  29. refreshPlaces(list);
  30. } catch (JSONException e) {
  31. Log.e(TAG, "Error occurred while getting places", e);
  32. }
  33. Log.d(TAG, "onResponse: " + response.substring(0,250));
  34. }
  35. }, new Response.ErrorListener() {
  36. @Override
  37. public void onErrorResponse(VolleyError error) {
  38. }
  39. });
  40.  
  41. // Add the request to the RequestQueue.
  42. queue.add(stringRequest);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement