2012-10-24 106 views
5

固定鏈接獲取的地方,現在我得到一個JSON錯誤味精從Foursquare的API

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

tomcat的消息

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 
+0

將您的logcat – Praveenkumar

回答

1

url連接()返回一個空的結果,因爲這樣的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");