簡體   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