Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6186

Android: error in fetching place address using foursquare integration

i am working in android. i want to show list of address of places. i am doing this task using foursquare.

before some time it is working fine. but now it showing some error.

this is the piece of code which i am trying.

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;
}

this is the error which is generating:-

org.json.JSONException: No value for address org.json.JSONObject.get(JSONObject.java:354)

I checked out the OUTPUT of API which i am using on browser. this is the output of that 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}}]}}

please suggest me what should i do for this. as i think this is related to version of API problem.

Upvotes: 0

Views: 924

Answers (1)

Jonathan Levison
Jonathan Levison

Reputation: 2617

There are two problems:

  1. API usage, you need to add &v={date} to the request, as explained at that link you got (here) .. example: v=20111228 is today
  2. The exception, that venue does not have an address input to it, so foursquare will not return it in the json response. When parsing the response ensure that the data you are looking for was returned before accessing it. This is true for most data returned by Foursquare - it will not be included in the response if it does not exist.

So add the &v= to the request and validate that the data exists before accessing it, should solve the issue.

Upvotes: 3

Related Questions