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 Build a Blog Reader Android App Adapting Data for Display in a List Filling Our String Array and Creating the Adapter

Monzer Selim
Monzer Selim
Courses Plus Student 3,849 Points

Blog data is not showing after writing exact code in the tutorial

here is my code copy .... plz check what is wrong with it :

package com.blogspot.monapps.blogreader;

import android.app.AlertDialog; 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.text.Html; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.Toast;

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

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;

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 JSONObject mBlogData;

protected void onCreate(Bundle savedInstanceState) {

    if (isNetworkAvailable()) {
        GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
    else {
        Toast.makeText(this,"Network is unavailable",Toast.LENGTH_LONG).show();

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

private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager)
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if (networkInfo !=null && networkInfo.isConnected()) {
        isAvailable = true;
    return isAvailable;

} public void updateList() { if (mBlogData == null){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.error_title)); builder.setMessage(getString(R.string.error_message)); builder.setPositiveButton(android.R.string.ok , null); AlertDialog dialog = builder.create(); dialog.show(); } else{ try { JSONArray jsonPosts = mBlogData.getJSONArray("posts"); mBlogPostTitles = new String[jsonPosts.length()]; for (int i = 0 ; i<jsonPosts.length();i++){ JSONObject post = jsonPosts.getJSONObject(i); String title = post.getString("title"); title = Html.fromHtml(title).toString(); mBlogPostTitles [i] = title;} ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1, mBlogPostTitles); setListAdapter(adapter); } catch (JSONException e) { Log.e(TAG, "Exception Caught!", e); } } }

private class GetBlogPostsTask extends AsyncTask {

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

            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];
               String responseData = new String(charArray);
               jsonResponse = new JSONObject(responseData);



           Log.i(TAG, "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 jsonResponse;

    public void onPostExecute(JSONObject result){
        mBlogData = result;