繁体   English   中英

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

[英]How to fix JSONException: Index 0 out of range [0..0)?

我试图获取latlng的的orderRequest地址显示来自其中的管理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)

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

我希望这有帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM