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 trialJennifer Chance
Courses Plus Student 1,424 PointsOnly (No items to display) shows when running app.
Instead of their being an array of blogpost when running my app it shows(No items to display). I have exact code as shown in the tutorials. Any help would be appreciated.
This is my source code
package com.jerdeez.blogreader;
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;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
if (isNetworkAvailable()) {
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
//Toast.makeText(this, getString(R.string.no_items),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Network is unavailable!", 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) {
//to do: handle error
}
else{
try {
JSONArray jsonPost = mBlogData.getJSONArray("post");
mBlogPostTitles = new String[jsonPost.length()];
for (int i =0; i<jsonPost.length(); i++){
JSONObject post = jsonPost.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[] objects) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
//URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count =" + NUMBER_OF_POSTS);
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
//Creating are response code so that we can get data from the internet
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream(); //store the data into the input stream
Reader reader = new InputStreamReader(inputStream);//read the input stream
int contentLength = connection.getContentLength();//get the number of characters to read in
char[] charArray = new char[contentLength];//create the char array to store the the data
reader.read(charArray); //read and store the data array into the char array
String responseData = new String(charArray);//create a new string and convert and store from char to string
//This creates are Json object
jsonResponse = new JSONObject(responseData);
}
//} else {
//Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
//}
Log.i(TAG, "Code: " + responseCode);
} catch (MalformedURLException e) {
//e.printStackTrace();
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;
}
@Override
protected void onPostExecute(JSONObject result) {
mBlogData = result;
updateList();
}
}
}
This is my logcat
10-15 11:25:13.919 32029-32029/com.jerdeez.blogreader W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
10-15 11:25:13.929 32029-32029/com.jerdeez.blogreader W/IInputConnectionWrapper﹕ setComposingText on inactive InputConnection
10-15 11:25:19.039 32029-32029/com.jerdeez.blogreader W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
10-15 11:25:19.319 32029-32029/com.jerdeez.blogreader I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2087>: Format RGBA_8888.
10-15 11:25:19.679 32029-32284/com.jerdeez.blogreader I/MainListActivity﹕ Code: 200
10-15 11:25:19.679 32029-32029/com.jerdeez.blogreader E/MainListActivity﹕ Exception Caught!
org.json.JSONException: No value for post
at org.json.JSONObject.get(JSONObject.java:354)
at org.json.JSONObject.getJSONArray(JSONObject.java:544)
at com.jerdeez.blogreader.MainListActivity.updateList(MainListActivity.java:77)
at com.jerdeez.blogreader.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:148)
at com.jerdeez.blogreader.MainListActivity$GetBlogPostsTask.onPostExecute(MainListActivity.java:97)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4781)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:3152)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:2919)
at dalvik.system.NativeStart.main(Native Method)
1 Answer
Gloria Dwomoh
13,116 PointsI must say your error was really hard to find. I am not sure if I got it, as there could be more, but...try changing
JSONArray jsonPost = mBlogData.getJSONArray("post");
to
JSONArray jsonPost = mBlogData.getJSONArray("posts");
Ps. If you have errors, it is almost sure something in your code is not exactly the same as shown in the tutorials. So, it needs to be carefully written.
Jennifer Chance
Courses Plus Student 1,424 PointsGloria,
Thanks you are awesome!! After i have changed my code around with frustration and the whole time it was the "Post" instead of "Posts" LOL as you can probably tell i'm quite the newb.
Gloria Dwomoh
13,116 PointsNo problem XD that is hard for any type of person to find. No matter the experience. Because it is more of a "typo" than a coding issue. You are welcome. Good luck.
Kate Hoferkamp
5,205 PointsKate Hoferkamp
5,205 PointsWhat emulator are you using?