org.json.JSONException:overview_polyLine 没有值

org.json.JSONException: No value for overview_polyLine

我正在创建一个应用程序,它绘制一条从用户当前位置到用户输入的目的地位置的路线。

在使用方向 API 时,我从 API 获取数据,但出现错误 "org.json.JSONException: No value for overview_polyLine"


    private void getDirection() {

        // currentPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
        if (mLastLocation != null) {
            currentPosition = new LatLng (mLastLocation.getLatitude (), mLastLocation.getLongitude ());
        }

        String requestApi;
        try {
            requestApi = "https://maps.googleapis.com/maps/api/directions/json?" + "mode=driving&" + "transit_routing_preference=less_driving&" +
                    "origin=" + currentPosition.latitude + "," + currentPosition.longitude + "&" +
                    "destination=" + destination + "&" +
                    "key=" + getResources ().getString (R.string.google_direction_api);
            Log.d ("Ashutosh", requestApi);
            mService.getPath (requestApi)
                    .enqueue (new Callback<String> () {
                        @Override
                        public void onResponse(Call<String> call, Response<String> response) {
                            try {

                                JSONObject jsonObject = new JSONObject (response.body ().toString ());
                                JSONArray jsonArray = jsonObject.getJSONArray ("routes");
                                JSONObject route = jsonArray.getJSONObject (0);
                                JSONObject poly = route.getJSONObject ("overview_polyLine");
                                String polyline = poly.getString ("points");
                                polyLineList = decodePoly (polyline);
                                if (!polyLineList.isEmpty ()) {
                                    LatLngBounds.Builder builder = new LatLngBounds.Builder ();
                                    for (LatLng latLng : polyLineList) {
                                        builder.include (latLng);
                                    }
                                    LatLngBounds bounds = builder.build ();
                                    CameraUpdate mCameraUpdate = CameraUpdateFactory.newLatLngBounds (bounds, 2);
                                    mMap.animateCamera (mCameraUpdate);


                                }

                                polylineOptions = new PolylineOptions ();
                                polylineOptions.color (Color.BLUE);
                                polylineOptions.width (5);
                                polylineOptions.startCap (new SquareCap ());
                                polylineOptions.endCap (new SquareCap ());
                                polylineOptions.jointType (JointType.ROUND);
                                polylineOptions.addAll (polyLineList);
                                greyPolyline = mMap.addPolyline (polylineOptions);

                                blackpolyLineOptions = new PolylineOptions ();
                                blackpolyLineOptions.color (Color.BLACK);
                                blackpolyLineOptions.width (5);
                                blackpolyLineOptions.startCap (new SquareCap ());
                                blackpolyLineOptions.endCap (new SquareCap ());
                                blackpolyLineOptions.jointType (JointType.ROUND);
                                blackPolyline = mMap.addPolyline (blackpolyLineOptions);

                                mMap.addMarker (new MarkerOptions ()
                                        .position (polyLineList.get (polyLineList.size () - 1))
                                        .title ("Pickup Location"));
                                //Animation
                                ValueAnimator polylineAnimator = ValueAnimator.ofInt (0, 100);
                                polylineAnimator.setDuration (2000);
                                polylineAnimator.setInterpolator (new LinearInterpolator ());
                                polylineAnimator.addUpdateListener (new ValueAnimator.AnimatorUpdateListener () {
                                    @Override
                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        List<LatLng> points = greyPolyline.getPoints ();
                                        int percentValue = (int) animation.getAnimatedValue ();
                                        int size = points.size ();
                                        int new_Points = (int) (size * (percentValue / 100.0f));
                                        List<LatLng> p = points.subList (0, new_Points);
                                        blackPolyline.setPoints (p);
                                    }
                                });
                                polylineAnimator.start ();
                                carMarker = mMap.addMarker (new MarkerOptions ().position (currentPosition)
                                        .flat (true)
                                        .icon (BitmapDescriptorFactory.fromResource (R.drawable.car)));

                                handler = new Handler ();
                                index = -1;
                                next = 1;
                                handler.postDelayed (drawPathRunnable, 3000);

                            } catch (JSONException e) {
                                e.printStackTrace ();
                            }
                        }

                        @Override
                        public void onFailure(Call<String> call, Throwable t) {
                            Toast.makeText (MapsActivity.this, "" + t.getMessage (), Toast.LENGTH_SHORT).show ();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

我正在从 API 获取数据,但由于这个错误无法在 google 地图上显示。

来自 Logcat 的错误。

2019-10-15 09:41:23.007 13728-13728/com.example.uber W/System.err: org.json.JSONException: No value for overview_polyLine
2019-10-15 09:41:23.007 13728-13728/com.example.uber W/System.err:     at org.json.JSONObject.get(JSONObject.java:392)
2019-10-15 09:41:23.007 13728-13728/com.example.uber W/System.err:     at org.json.JSONObject.getJSONObject(JSONObject.java:612)
2019-10-15 09:41:23.007 13728-13728/com.example.uber W/System.err:     at com.example.uber.MapsActivity.onResponse(MapsActivity.java:299)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:70)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:101)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at android.os.Looper.loop(Looper.java:166)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7425)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

按键区分大小写。实际键名是 overview_polyline 但不是 overview_polyLine