需要解析来自Google Directions API的JSON响应才能检索“机动”

时间:2017-05-10 19:06:50

标签: android json api navigation

我正在制作一个应该引导用户(导航)的Android应用程序。我创建了一个地图活动,地图工作正常。我可以向Google Directions API发送请求并获取JSON响应。 JSON响应在单独的“DataParser”类中解析。 “解析”方法能够从每个步骤读取“折线”对象,对其进行解码并绘制与地图上的折线点对应的线。我需要Parser从每一步中检索字符串“maneuver”并返回HashMap(起始位置,机动)。但是,当我尝试在备用“parse1”方法中执行此操作时,从不检索与每个步骤中的“操作”对应的字符串。这是DataParser代码:

package com.example.application.googlemapsdrawroute;

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataParser {

/** Receives a JSONObject and returns a list of lists containing latitude 
and longitude */
public List<List<HashMap<String,String>>> parse(JSONObject jObject){

    List<List<HashMap<String, String>>> routes = new ArrayList<>() ;
    JSONArray jRoutes;
    JSONArray jLegs;
    JSONArray jSteps;

    try {

        jRoutes = jObject.getJSONArray("routes");

        /** Traversing all routes */
        for(int i=0;i<jRoutes.length();i++){
            jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
            List path = new ArrayList<>();

            /** Traversing all legs */
            for(int j=0;j<jLegs.length();j++){
                //String navig = "";
                jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");

                /** Traversing all steps */
                for(int k=0;k<jSteps.length();k++){
                    String polyline = "";
                    polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
                    //navig= (String)((JSONObject)jSteps.get(k)).get("maneuver");
                    List<LatLng> list = decodePoly(polyline);

                    /** Traversing all points */
                    for(int l=0;l<list.size();l++){
                        HashMap<String, String> hm = new HashMap<>();
                        hm.put("lat", Double.toString((list.get(l)).latitude) );
                        hm.put("lng", Double.toString((list.get(l)).longitude) );
                        path.add(hm);
                    }
                }
                routes.add(path);
            }
        }

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


    return routes;
}


public HashMap<LatLng,String> parse1(JSONObject jObject){

    HashMap<LatLng, String> navig = new HashMap<>() ;
    JSONArray jRoutes;
    JSONArray jLegs;
    JSONArray jSteps;

    try {

        jRoutes = jObject.getJSONArray("routes");

        /** Traversing all routes */
        for(int i=0;i<jRoutes.length();i++){
            jLegs = ((JSONObject)jRoutes.get(i)).getJSONArray("legs");
            List path = new ArrayList<>();

            /** Traversing all legs */
            for(int j=0;j<jLegs.length();j++){
                String navig1 = "";
                jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");

                /** Traversing all steps */
                for(int k=0;k<jSteps.length();k++){
                    //String polyline = "";

                    LatLng startpt = new LatLng((Double)((JSONObject)((JSONObject)jSteps.get(k)).get("start_location")).get("lat"),(Double)((JSONObject)((JSONObject)jSteps.get(k)).get("start_location")).get("lng"));
                    navig1= ((JSONObject) jSteps.get(k)).getString("maneuver");
                    Log.d("Navig", navig1);
                    Log.d("LatLng", startpt.toString());

                    //List<LatLng> list = decodePoly(polyline);

                    /** Traversing all points
                    for(int l=0;l<list.size();l++){
                        HashMap<String, String> hm = new HashMap<>();
                        hm.put("lat", Double.toString((list.get(l)).latitude) );
                        hm.put("lng", Double.toString((list.get(l)).longitude) );
                        path.add(hm);
                    }*/
                    navig.put(startpt,navig1);
                }
                //routes.add(path);
            }
        }

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


    return navig;
}

/**
 * Method to decode polyline points
 * Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
 * */
private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;

        LatLng p = new LatLng((((double) lat / 1E5)),
                (((double) lng / 1E5)));
        poly.add(p);
    }

    return poly;
   }
}

运行此代码时logcat中的响应是:

2432-4224/com.example.application.googlemapsdrawroute W/System.err:     at com.example.application.googlemapsdrawroute.DataParser.parse1(DataParser.java:94)

和DataParser类中的第94行是:

navig1= ((JSONObject) jSteps.get(k)).getString("maneuver");

这是错的吗?如果是这样,我如何从每个步骤中的“机动”中检索字符串?

1 个答案:

答案 0 :(得分:0)

只需在第94行之前添加if语句

    if((((JSONObject) jSteps.get(k))).get("maneuver")!=null)

它会起作用。