Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

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

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main_list);


    if (isNetworkisAvailable()){

        GetBlogPostTask getBlogPostTask = new GetBlogPostTask();

        getBlogPostTask.execute();

    }

    else{

        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) 

            getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = manager.getActiveNetworkInfo();


    boolean isAvailable = false;


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

        isAvailable = true;

    }

    return isAvailable;

}

@Override

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> {

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

            //--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];

                reader.read(charArray);

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

            }

            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;

        }
    }

}```

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!!