0

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
4

1 に答える 1

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

しかし、私は別の質問で別の問題を尋ねています

于 2013-03-01T16:52:14.787 に答える