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

Java 如何修复JSONException:索引0超出范围[0..0]?,java,android,json,google-maps,jsonexception,Java,Android,Json,Google Maps,Jsonexception,我正在尝试获取orderRequest地址的lat和lng,以显示来自orderRequest的管理员 private void drawRoute(LatLng yourLocation, String address) { mServices.getGoeCode(address).enqueue(new Callback<String>() { @Override public void onResponse(Call<Strin

我正在尝试获取
orderRequest
地址的
lat
lng
,以显示来自
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) {

        }
    });
}
private void drawRoute(位置、字符串地址){
mServices.getGoeCode(address).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
试一试{
JSONObject=newJSONObject(response.body().toString());
字符串lat=((JSONArray)jsonObject.get(“结果”))
.getJSONObject(0)
.getJSONObject(“几何体”)
.getJSONObject(“位置”)
.get(“lat”).toString();
字符串lng=((JSONArray)jsonObject.get(“结果”))
.getJSONObject(0)
.getJSONObject(“几何体”)
.getJSONObject(“位置”)
.get(“lng”).toString();
LatLng employeeLocation=新LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.box);
位图=Common.scaleBitmap(位图,70,70);
MarkerOptions MarkerOptions=新建MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(位图))
.头衔(“当前人的顺序”)
.职位(员工所在地);
mMap.addMarker(markerOptions);
}捕获(JSONException e){
Toast.makeText(MapsActivity.this,“错误:+e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}

管理员希望看到order loaction,但输出是JSONException:索引0超出范围[0..0)

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

在处理JSON对象之前,您必须检查响应的状态。请注意,状态可能是零结果、超过查询限制或其他。您可以在地理编码API web服务的文档中找到可能状态的完整列表:

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

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

我希望这能有所帮助。

发布您的JSON。它听起来像是空的。以“我猜…”开头的建议应该是注释。