如何修复JSONException:索引0超出[0..0)范围?

时间:2019-07-28 07:17:27

标签: java android json google-maps jsonexception

我正在尝试获取lat地址中的lngorderRequest,以向管理员显示OrderRequest的来源。

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) {

        }
    });
}

管理员希望看到订单记录,但输出为JSONException:索引0超出[0..0)范围

2 个答案:

答案 0 :(得分:0)

在处理JSON对象之前,您必须检查响应的状态。请注意,状态可能为ZERO_RESULTS或OVER_QUERY_LIMIT或其他。您可以在Geocoding API Web服务的文档中找到可能状态的完整列表:

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

我建议将您的代码更改为以下内容

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();
}

我希望这会有所帮助。

答案 1 :(得分:-1)

我猜您的“结果”数组没有元素,请尝试在使用前测试其长度。