修正了链接,现在我收到了一个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);
}
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 :(得分:1)
使用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");