我正在使用Foursquare的API列出所有类别,包含以下代码
HttpGet httpGet = new HttpGet(FOURSQUARE_CAT);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = null;
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = r.readLine()) != null) {
stringBuilder.append(line);
}
jsonObject = new JSONObject(stringBuilder.toString());
String str = "";
if(jsonObject !=null){
Log.d("cat0", stringBuilder.toString());
jsonObject = jsonObject.getJSONObject("response");
jsonArray = ((JSONArray)jsonObject.get("categories"));//.getJSONObject(0).getString("formatted_address");
Log.d("cat1", jsonArray.toString());
Log.d("cat2length", jsonArray.length() + "");
for(int i = 0; i < jsonArray.length(); i++){
if(jsonArray.getJSONObject(i).get("name").toString().equalsIgnoreCase("food")){
str = jsonArray.getJSONObject(i).get("id").toString();
Log.d("cat loop ", i + " out of " + jsonArray.length());
break;
}
}
Message.obtain(mHandler, UPDATE_ADDRESS, str).sendToTarget();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.d("cpe", e.toString());
}
catch (IOException e) {
e.printStackTrace();// Log.d("ioe", "");
Log.d("ioe", e.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("jsone", e.toString());
}
我输入那些日志命令来查看我的输入和输出是否符合预期,并且出于某种原因,整个try catch块保持循环。我尝试取出for循环但确认不是原因
我对更新代码的堆栈跟踪如下所示,请注意它没有返回完整的JSON数据,我通过比较从代码和浏览器中获得的内容来验证这一点:
03-01 18:50:46.463: I/global(316): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
03-01 18:50:47.433: D/dalvikvm(316): GC_FOR_MALLOC freed 18169 objects / 1570256 bytes in 57ms
03-01 18:50:47.442: D/NativeCrypto(316): Freeing OpenSSL session
03-01 18:50:47.752: D/dalvikvm(316): GC_FOR_MALLOC freed 1530 objects / 711792 bytes in 39ms
03-01 18:50:48.042: D/dalvikvm(316): GC_FOR_MALLOC freed 6921 objects / 486480 bytes in 47ms
03-01 18:50:48.072: D/cat0(316): {"meta":{"code":200},"response":{"categories": [{"id":"4d4b7104d754a06370d81259","name":"Arts & Entertainment","pluralName":"Arts & Entertainment","shortName":"Arts & Entertainment","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_","suffix":".png"},"categories":[{"id":"4fceea171983d5d06c3e9823","name":"Aquarium","pluralName":"Aquariums","shortName":"Aquarium","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/aquarium_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d1e1931735","name":"Arcade","pluralName":"Arcades","shortName":"Arcade","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/arcade_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d1e2931735","name":"Art Gallery","pluralName":"Art Galleries","shortName":"Art Gallery","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/artgallery_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d1e4931735","name":"Bowling Alley","pluralName":"Bowling Alleys","shortName":"Bowling Alley","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/bowling_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d17c941735","name":"Casino","pluralName":"Casinos","shortName":"Casino","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/casino_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d18e941735","name":"Comedy Club","pluralName":"Comedy Clubs","shortName":"Comedy Club","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/comedyclub_","suffix":".png"},"categories":[]},{"id":"5032792091d4c4b30a586d5c","name":"Concert Hall","pluralName":"Concert Halls","shortName":"Concert Hall","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/musicvenue_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d1f1931735","name":"General Entertainment","pluralName":"General Entertainment","shortName":"Other - Entertainment","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/default_","suffix":".png"},"categories":[]},{"id":"4deefb944765f83613cdba6e","name":"Historic Site","pluralName":"Historic Sites","shortName":"Historic Site","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/historicsite_","suffix":".png"},"categories":[]},{"id":"4bf58dd8d48988d17f941735","name":"Movie Theater","pluralName":"Movie Theaters","shortName":"Movie Theater","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/movietheater_","suffix":".png"},"categories":[{"id":"4bf58dd8d48988d17e941735","name":"Indie Movie Theater","pluralName":"Indie Movie Theaters","shortName":"Indie Theater","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/movietheater_indietheater_","suffix":".png"}},{"id":"4bf58dd8d48988d180941735","name":"Multiplex","pluralName":"Multiplexes","shortName":"Cineplex","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/movietheater_cineplex_","suffix":".png"}}]},{"id":"4bf58dd8d48988d181941735","name":"Museum","pluralName":"Museums","shortName":"Museum","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/museum_","suffix":".png"},"categories":[{"id":"4bf58dd8d48988d18f941735","name":"Art Museum","pluralName":"Art Museums","shortName":"Art Museum","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/museum_art_","suffix":".png"}},{"id":"4bf58dd8d48988d190941735","name":"History Museum","pluralName":"History Museums","shortName":"History Museum","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/museum_history_","suffix":".png"}},{"id":"4bf58dd8d48988d192941735","name":"Planetarium","pluralName":"Planetariums","shortName":"Planetarium","icon":{"prefix":"https:\/\/foursquare.com\/img\/categories_v2\/arts_entertainment\/museum_planetarium_","suf
答案 0 :(得分:0)
我设法发现我应该使用以下代码
urlCat = new URL(FOURSQUARE_CAT);
Log.d("url", FOURSQUARE_CAT);
URLConnection httpGet = urlCat.openConnection();
httpGet.setConnectTimeout(1000);
httpGet.setDoInput(true);
httpGet.setDoOutput(true);
httpGet.connect();
但我在另一个问题中提出了不同的问题