简体   繁体   English

提取Google Places API响应的状态代码

[英]Extracting STATUS code of Google Places API response

I am new to using JSON and I am having an issue figuring out how to extract the STATUS code from Google Places API response. 我是使用JSON的新手,但是在弄清楚如何从Google Places API响应中提取STATUS代码时遇到问题。 I guess the issue is because it is a seperate root element from the results array. 我猜这是因为它是结果数组中单独的根元素。 Here is my code for my parser: 这是我的解析器代码:

public class PlaceJSONParser {

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

    JSONArray jPlaces = null;
    try {
        /** Retrieves all the elements in the 'places' array */
        jPlaces = jObject.getJSONArray("results");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    /** Invoking getPlaces with the array of json object
    * where each json object represent a place
    */
    return getPlaces(jPlaces);
}

private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
    int placesCount = jPlaces.length();
    List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> place = null;

    /** Taking each place, parses and adds to list object */
    for(int i=0; i<placesCount;i++){
        try {
            /** Call getPlace with place JSON object to parse the place */
            place = getPlace((JSONObject)jPlaces.get(i));
            placesList.add(place);

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

    return placesList;
}

/** Parsing the Place JSON object */
private HashMap<String, String> getPlace(JSONObject jPlace){

    HashMap<String, String> place = new HashMap<String, String>();
    String placeName = "-NA-";
    String vicinity="-NA-";
    String latitude="";
    String longitude="";

    try {
        // Extracting Place name, if available
        if(!jPlace.isNull("name")){
            placeName = jPlace.getString("name");
        }

        // Extracting Place Vicinity, if available
        if(!jPlace.isNull("vicinity")){
            vicinity = jPlace.getString("vicinity");
        }

        latitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
        longitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");

        place.put("place_name", placeName);
        place.put("vicinity", vicinity);
        place.put("lat", latitude);
        place.put("lng", longitude);

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

Yes, status, results and html_attributions are the 3 root elements in google places api JSON response. 是的,状态,结果和html_attributions是Google Places api JSON响应中的3个根元素。 You can extract the status data from the response like below: 您可以像下面这样从响应中提取状态数据:

String statuscheck = jObject.getString("status");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ERROR_OPERATION_FAILED在Android上使用Google Places Api的状态代码 - ERROR_OPERATION_FAILED Status Code on using Google Places Api in Android Google Places API返回空响应 - google places API returns empty response Google通过邮政编码放置API,自动填充功能 - Google places API, autocomplete with postal code Google将api放置在android中的示例代码 - sample code for google places api in android Android应用程序中的Google Places API:getGoogleAppId失败,状态为10 - Google Places API in Android app : getGoogleAppId failed with status 10 Android Google Places API,getAutocompletePredictions返回状态为“ PLACES_API_ACCESS_NOT_CONFIGURED” - Android Google Places API, getAutocompletePredictions returns status 'PLACES_API_ACCESS_NOT_CONFIGURED' 错误“状态{statusCode = PLACES_API_INVALID_APP,resolution = null}”在Android中使用google places api时 - Error “Status{statusCode=PLACES_API_INVALID_APP, resolution=null}” when using google places api in android 如何从android中的Google Places API获取JSON响应? - How to get JSON response from google places API in android? Google Places API。 PlaceLikelihoodBuffer响应缓慢。 Android的 - Google Places API. PlaceLikelihoodBuffer slow response. Android 在Ionic中点击来自Google Search Places API的响应 - Tap on Response from Google Search Places API in Ionic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM