Android:使用foursquare集成获取地址时出错

时间:2011-12-28 07:03:44

标签: android foursquare

我在android工作。我想显示地方的地址列表。我正在使用foursquare完成这项任务。

在一段时间之前它工作正常。但现在它显示出一些错误。

这是我正在尝试的一段代码。

public ArrayList<FsqVenue> getNearby(double latitude, double longitude) throws Exception {
    ArrayList<FsqVenue> venueList = new ArrayList<FsqVenue>();

    try {

        String ll   = String.valueOf(latitude) + "," + String.valueOf(longitude);
        URL url     = new URL(API_URL + "/venues/search?ll=" + ll + "&oauth_token=" + mAccessToken);

        Log.d(TAG, "Opening URL " + url.toString());

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        urlConnection.connect();

        String response     = streamToString(urlConnection.getInputStream());
        JSONObject jsonObj  = (JSONObject) new JSONTokener(response).nextValue();

        JSONArray groups    = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("groups");



        int length          = groups.length();

        if (length > 0) {
            for (int i = 0; i < length; i++) {
                JSONObject group    = (JSONObject) groups.get(i);
                JSONArray items     = (JSONArray) group.getJSONArray("items");

                int ilength         = items.length();

                for (int j = 0; j < 8; j++) {
                    JSONObject item = (JSONObject) items.get(j);

                    FsqVenue venue  = new FsqVenue();

                    venue.id        = item.getString("id");
                    venue.name      = item.getString("name");

                    JSONObject location = (JSONObject) item.getJSONObject("location");

                    Location loc    = new Location(LocationManager.GPS_PROVIDER);

                    loc.setLatitude(Double.valueOf(location.getString("lat")));
                    loc.setLongitude(Double.valueOf(location.getString("lng")));

                    venue.location  = loc;
                    venue.address   = location.getString("address");
                    venue.distance  = location.getInt("distance");
                    venue.herenow   = item.getJSONObject("hereNow").getInt("count");
                    venue.type      = group.getString("type");

                    venueList.add(venue);
                }
            }
        }






    } catch (Exception ex) {
        throw ex;
    }

    return venueList;
}

这是产生的错误: -

  

org.json.JSONException:没有地址值   org.json.JSONObject.get(JSONObject.java:354)

我查看了我在浏览器上使用的API的OUTPUT。这是该API的输出: -

{"meta":{"code":200,"errorType":"deprecated","errorDetail":"This endpoint will stop returning groups in the future. Please use a current version, see http:\/\/bit.ly\/vywCav."}**,"notifications":[{"type":"notificationTray","item":{"unreadCount":0}}],"response":{"groups":[{"type":"nearby","name":"Nearby","items":[{"id":"4ed0c8f48231b9ef88fe5f09","name":"Banayan Tree School","contact":{},"location":{"lat":26.857693,"lng":75.76603,"distance":524},"categories":[{"id":"4bf58dd8d48988d1a8941735","name":"General College & University","pluralName":"General Colleges & Universities","shortName":"Other - Education","icon":"https:\/\/foursquare.com\/img\/categories\/education\/default.png","parents":["Colleges & Universities"],"primary":true}],"verified":false,"stats":{"checkinsCount":3,"usersCount":3,"tipCount":0},"hereNow":{"count":0}}]}}

请建议我该怎么做。 因为我认为这与API问题的版本有关。

1 个答案:

答案 0 :(得分:3)

有两个问题:

  1. API使用情况,您需要在请求中添加&amp; v = {date},如您所获得的链接(here)所述。例如:v = 20111228今天
  2. 例外,该场地没有输入地址,因此foursquare不会在json响应中返回它。解析响应时,请确保在访问之前返回了您要查找的数据。对于Foursquare返回的大多数数据都是如此 - 如果它不存在,它将不会包含在响应中。
  3. 因此,将&amp; v =添加到请求并在访问之前验证数据是否存在,应解决问题。