0

I am trying to fetch lat and lng of the orderRequest address to show the admin from where the OrderRequest is coming.

private void drawRoute(LatLng yourLocation, String address) {

    mServices.getGoeCode(address).enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            try{
                JSONObject jsonObject = new JSONObject(response.body().toString());

                String lat = ((JSONArray)jsonObject.get("results"))
                                        .getJSONObject(0)
                                        .getJSONObject("geometry")
                                        .getJSONObject("location")
                                        .get("lat").toString();

                String lng = ((JSONArray)jsonObject.get("results"))
                        .getJSONObject(0)
                        .getJSONObject("geometry")
                        .getJSONObject("location")
                        .get("lng").toString();

                LatLng employeeLocation = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));

                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
                bitmap = Common.scaleBitmap(bitmap,70,70);
                MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                        .title("Order of current person")
                        .position(employeeLocation);
                mMap.addMarker(markerOptions);

            }catch (JSONException e){

                Toast.makeText(MapsActivity.this, "Error : "+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {

        }
    });
}

Admin expecting to see order loaction but output is JSONException: Index 0 out of range [0..0)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

You have to check the status of the response before you process a JSON object. Please note that status might be ZERO_RESULTS or OVER_QUERY_LIMIT or something else. You can find a complete list of possible statuses in the documentation of Geocoding API web service:

https://developers.google.com/maps/documentation/geocoding/intro#StatusCodes

I would suggest changing your code to the following

try {
  JSONObject jsonObject = new JSONObject(response.body().toString());

  //Get status first
  String status = jsonObject.getString("status");

  if (status.equals("OK")) {
    String lat = ((JSONArray)jsonObject.get("results"))
        .getJSONObject(0)
        .getJSONObject("geometry")
        .getJSONObject("location")
        .get("lat").toString();

    String lng = ((JSONArray)jsonObject.get("results"))
        .getJSONObject(0)
        .getJSONObject("geometry")
        .getJSONObject("location")
        .get("lng").toString();

    LatLng employeeLocation = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.box);
    bitmap = Common.scaleBitmap(bitmap,70,70);
    MarkerOptions markerOptions = new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
        .title("Order of current person")
        .position(employeeLocation);
    mMap.addMarker(markerOptions);
  } else if (status.equals("INVALID_REQUEST")) {
    //TODO: handle invalid request
  } else if (status.equals("ZERO_RESULTS")) {
    //TODO: handle zero results
  } else if (status.equals("OVER_QUERY_LIMIT")) {
    //TODO: handle over query limit
  }
  .....

} catch (JSONException e) {
    Toast.makeText(MapsActivity.this, "Error : "+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}

I hope this helps.

xomena
  • 31,125
  • 6
  • 88
  • 117
-1

I guess your "results" array has no elements, try to test its length before use.

forrestg
  • 327
  • 5
  • 13