0

I'm making use of Foursquare's API to list all the categories with the following code

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

i put in those log commands to see my inputs and outputs are as expected and for some reason, the whole try catch block keeps looping. i tried taking out the for loop but has confirmed that is not the cause

the stack trace that i have with the updated code is as below, note that it does not return a complete JSON data and i verified this by comparing what I get from the code and from the browser:

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
acvon
  • 161
  • 3
  • 6
  • 21
  • 1
    The last part of your question is totally unclear. Include in your code snippet, the end of the try block and its catch block(s) as well. Also include any stack trace you may be getting. – Perception Feb 28 '13 at 17:35
  • i updated the question already – acvon Mar 01 '13 at 10:57

1 Answers1

0

I manage to find out that i should just use the following code

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

but i have a different problem asked in a different question

acvon
  • 161
  • 3
  • 6
  • 21