【问题标题】:How to fix JSONException: Index 0 out of range [0..0)?如何修复 JSONException:索引 0 超出范围 [0..0)?
【发布时间】:2019-12-05 21:39:52
【问题描述】:

我正在尝试获取orderRequest 地址的latlng,以向管理员显示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: Index 0 out of range [0..0)

【问题讨论】:

  • 发布您的 JSON。听起来好像是空的。

标签: java android json google-maps jsonexception


【解决方案1】:

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

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Advice 以 "I guess .." 开头的应该是 cmets。
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 2016-12-16
    • 2012-04-07
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多