Android 从Foursquare API获取位置

Android 从Foursquare API获取位置,android,foursquare,Android,Foursquare,修复了链接,现在我得到一个JSON错误消息 java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject 显然我的错误就在这里: String response = streamToString(urlConnection.getInputStream()); JSONObject jsonObj = (JSONObject) new JSONTok

修复了链接,现在我得到一个JSON错误消息

 java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to org.json.JSONObject
显然我的错误就在这里:

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);
                    }
使用url connection()返回一个空结果,因为这样json将无法解析空数据。另一种方法是使用googleclient Api,如下所示

        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");
        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");