org.json.JSONException:overview_polyLine 没有值

Posted

技术标签:

【中文标题】org.json.JSONException:overview_polyLine 没有值【英文标题】:org.json.JSONException: No value for overview_polyLine 【发布时间】:2020-02-11 16:43:33 【问题描述】:

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

在使用 Direction 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 获取数据,但由于此错误而无法在谷歌地图上显示。

来自 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$4.onResponse(MapsActivity.java:299)
2019-10-15 09:41:23.008 13728-13728/com.example.uber W/System.err:     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.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)

【问题讨论】:

【参考方案1】:

键区分大小写。实际密钥名称是 overview_polyline 但不是 overview_polyLine

【讨论】:

@Ashutoshpatole 很高兴为您提供帮助。接受它作为答案,因为它对其他人有帮助 @Ashutoshpatole 如果这有助于您解决问题,您可以接受此答案。它鼓励其他开发人员互相帮助。

以上是关于org.json.JSONException:overview_polyLine 没有值的主要内容,如果未能解决你的问题,请参考以下文章

org.json.JSONException:电子邮件没有值

org.json.JSONException:名称没有值

org.json.JSONException:overview_polyLine 没有值

类型 org.json.JSONException 被定义多次

java.lang.classnotfoundexception org.json.jsonexception

在尝试解析 JSON 数据 W/System.err 时出现此错误:org.json.JSONException:对客户没有价值