Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting places from Foursquare API

Fixed the link, now I get a JSON error msg

 java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject

Apparently my error lies somewhere here:

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 < ilength; 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.type      = group.getString("type");                         
                    VenueList.add(venue);
                    }

tomcat message

10-25 16:37:32.731: I/System.out(12828): https://api.foursquare.com/v2/venues/search?            ll=2.94,101.66&client_id=xxxxx&client_secret=xxxxxxx


10-25 16:37:32.731: D/FoursquareApi(12828): OPening URLhttps://api.foursquare.com/v2/venues/search?ll=2.94,101.66&client_id=xxxxxxx&client_secret=xxxxxxx
10-25 16:37:34.645: W/System.err(12828): java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject
10-25 16:37:34.645: W/System.err(12828):    at com.example.reddot.Foursquare.getNearby(Foursquare.java:57)
10-25 16:37:34.653: W/System.err(12828):    at com.example.reddot.MainActivity$LoadPlaces.doInBackground(MainActivity.java:466)
10-25 16:37:34.653: W/System.err(12828):    at com.example.reddot.MainActivity$LoadPlaces.doInBackground(MainActivity.java:1)
10-25 16:37:34.653: W/System.err(12828):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-25 16:37:34.653: W/System.err(12828):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-25 16:37:34.653: W/System.err(12828):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-25 16:37:34.661: W/System.err(12828):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-25 16:37:34.661: W/System.err(12828):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-25 16:37:34.661: W/System.err(12828):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-25 16:37:34.661: W/System.err(12828):    at java.lang.Thread.run(Thread.java:856)
10-25 16:37:34.661: I/System.out(12828): errorjava.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject
like image 558
Princewill Obinna Iwuorie Avatar asked Nov 13 '22 19:11

Princewill Obinna Iwuorie


1 Answers

using url connection() returns an empty result as such json will not be able to phrase empty data. the alternative to this is to use googleclient Api as follows

        HttpTransport transport = new NetHttpTransport();
        HttpRequestFactory httpRequestFactory = createRequestFactory   (HTTP_TRANSPORT);
        String ll = String.valueOf(df.format(lonlat))+","+String.valueOf(df.format(lonlat2));
        url = (API_URL+ "venues/search?ll="+ll+"&client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET+"&v"+v); 
        HttpRequest request = httpRequestFactory
                .buildGetRequest(new GenericUrl(url));


        System.out.println(request);
        HttpResponse httpResponse = request.execute();

        JSONObject object = new JSONObject(httpResponse.parseAsString());
        JSONObject foursquareResponse = (JSONObject) object.get("response");
        JSONArray groups    = (JSONArray) foursquareResponse.get("groups");
        JSONObject group = (JSONObject)groups.get(0);
        JSONArray items = (JSONArray)group.get("items");
like image 68
Princewill Obinna Iwuorie Avatar answered Nov 15 '22 11:11

Princewill Obinna Iwuorie