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!

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


Jeffrey Wambugu
Jeffrey Wambugu
8,548 Points

Only five items load on the logCat in the BlogReader app

Hi, I have been following along with the blog reader app and when i finished parsing the data to JSON and went to check the log cat there are only five items loaded instead of 20 as in Ben's example.

``` package com.wambugu.blogreader;

import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;

import org.json.JSONArray; import org.json.JSONObject;

import android.app.ListActivity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.Toast;

public class MainListActivity extends ListActivity { protected String[] mBlogPostTitles; public static final int NUMBER_OF_POSTS = 20; public static final String TAG = MainListActivity.class.getSimpleName();


protected void onCreate(Bundle savedInstanceState) {



    if (isNetworkisAvailable()){

        GetBlogPostTask getBlogPostTask = new GetBlogPostTask();




        Toast.makeText(this, "Network is Unavailabe", Toast.LENGTH_LONG).show();


        //Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();


private boolean isNetworkisAvailable() {

    ConnectivityManager manager = (ConnectivityManager) 


    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;

    if(networkInfo != null && networkInfo.isConnected()){

        isAvailable = true;


    return isAvailable;



public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main_list, menu);

return true;


private class GetBlogPostTask  extends AsyncTask<Object, Void, String> {

    protected String doInBackground(Object... arg0) {

        int responseCode = -1;


            URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);

            HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();

            responseCode = connection.getResponseCode();

            //--checking if the response is equal to the positive response code of 200--
            if (responseCode == HttpURLConnection.HTTP_OK) {

                InputStream inputStream = connection.getInputStream();

                Reader reader = new InputStreamReader(inputStream);

                int contentLength = connection.getContentLength();

                char[] charArray = new char[contentLength];


                String responseData = new String(charArray);

                JSONObject jsonResponse = new JSONObject(responseData);
                String status = jsonResponse.getString("status");
                Log.v(TAG, status);

                JSONArray jsonPosts = jsonResponse.getJSONArray("posts");

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



                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;



and the last few line shown on the log cat, the last four show the titles loaded but only five of them

03-21 12:30:29.707: D/dalvikvm(1290): GC_FOR_ALLOC freed 51K, 5% free 2946K/3072K, paused 30ms, total 36ms
03-21 12:30:29.707: I/dalvikvm-heap(1290): Grow heap (frag case) to 3.550MB for 635812-byte allocation
03-21 12:30:29.777: D/dalvikvm(1290): GC_FOR_ALLOC freed 2K, 4% free 3564K/3696K, paused 43ms, total 43ms
03-21 12:30:30.247: I/Choreographer(1290): Skipped 40 frames!  The application may be doing too much work on its main thread.
03-21 12:30:30.257: D/gralloc_goldfish(1290): Emulator without GPU emulation detected.
03-21 12:30:30.357: I/ActivityManager(371): Displayed com.wambugu.blogreader/.MainListActivity: +8s431ms
03-21 12:30:32.837: V/MainListActivity(1290): ok
03-21 12:30:32.847: V/MainListActivity(1290): Post: 0What We Learned at Treehouse This Week: CSS, Objective-C, Starting a Business and More
03-21 12:30:32.847: V/MainListActivity(1290): Post: 1Web Fonts, Code Guides, and JavaScript Promises | The Treehouse Show 82
03-21 12:30:32.857: V/MainListActivity(1290): Post: 2Exploring the JavaScript Device APIs
03-21 12:30:32.857: V/MainListActivity(1290): Post: 3What Did You Learn at Treehouse This Week?
03-21 12:30:32.857: V/MainListActivity(1290): Post: 4Gamepad Controls for HTML5 Games

1 Answer

On the line for(int i = 0; i<jsonResponse.length();i++), change jsonResponse.length() to jsonPosts.length()

Jeffrey Wambugu
Jeffrey Wambugu
8,548 Points

Thanks Ben... I was looping the wrong object!!