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
David Drees
9,219 PointsBlog Reader: Not Displaying Treehouse Posts or Toast Notification
Hello again forum,
I'm on the "Filling Our String and Creating the Adapter" part of the "Adapting Data for Display in a List."
I followed the video and when Ben get the Treehouse posts to show up in his emulator I got the "No Items To Display." text with no toast notification. I restarted eclipse and tried again as well as force stopping the app and reloading. No luck :(
Here is my mainActivity.java code:
package com.daviddrees.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.JSONException;
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.text.Html;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
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 JSONObject mBlogData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
if (isNetworkAvailable()) {
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
}
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)
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;
}
public void updateList() {
if (mBlogData == null) {
//TODO Handle Error
}
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<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... arg0) {
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();
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);
jsonResponse = new JSONObject(responseData);
}
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 jsonResponse;
}
}
protected void onPostExecute(JSONObject result) {
mBlogData = result;
updateList();
}
}
2 Answers

Ben Jakuben
Treehouse TeacherI pasted your code in my project and ran it in the debugger and found that onPostExecute()
was not being called. I looked at it for a minute trying to figure out why when I realized it's simply in the wrong place. You need to move it up inside the curly brace above it that closes off your GetBlogPostsTask class. It's an AsyncTask method, and if it's not there, it won't get called after the doInBackground()
method finishes.

ivicakartelo2
104 PointsBen Jakuben , on my mob Blog Reader: Oops! Sory..... there is no data in mBlogData. ???. In emulator working ok. My mob: Samsung Mini with Android 2.3.6 When I download your Blog Reader from Google Play, i have same problem. It means that I need sommething setup on my mob. What?

Ben Jakuben
Treehouse TeacherWhen you run it from Eclipse on your Samsung Mini, check the Logcat tab for any kind of errors. You may also need to run it with the debugger and step through the code where it's retrieving data from the web to see if you can determine where it's failing.