Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Android Blog Reader always return 10 items.

I just finished the Getting Data from the Web section for Android Blog Reader app. However, I only get 10 blog posts, even I set the count = 20.

Blow is my code:

    protected String doInBackground(Object... arg0) {
        int responseCode = -1;
        try {
            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
            HttpURLConnection connection = (HttpURLConnection)blogFeedUrl.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                Reader reader = new InputStreamReader(inputStream);
                int contentLength = connection.getContentLength();
                char[] charArray = new char[contentLength];
                reader.read(charArray);
                String responseData = new String(charArray);

                JSONObject jsonResponse = new JSONObject(responseData);
                String status = jsonResponse.getString("status");

                JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
                for (int i = 0; i < jsonPosts.length(); i++) {
                    JSONObject jsonPost = jsonPosts.getJSONObject(i);
                    String title = jsonPost.getString("title");
                    Log.v(TAG, "Post " + i + ": " + title);
                }

            }
            else {
                Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
            }
        }
        catch (MalformedURLException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (IOException e) {
            Log.e(TAG, "Exception caught: ", e);
        }
        catch (Exception e) {
            Log.e(TAG, "Exception caught: ", e);
        }

        return "Code: " + responseCode;
    }

1 Answer

Hi Ziyang, can you try and hard code "count=20" into the url and verify that you are getting 20 items returned in JSON. Maybe also within the try statement Log the value of NUMBER_OF_POSTS and see what it gives you.

Keep us updated!

I tried to hard code "count=20", still show only 10 items. I guess it might be a problem in the server side.