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 trialJames Bunn
1,112 PointsParsing Data Returned in JSON Format
My logcat view does not display any JSON messages. Any thoughts?
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 (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_item), 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;
}
private class GetBlogPostsTask 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();
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 < jsonPosts.length(); i++){
JSONObject jsonPost = jsonPosts.getJSONObject(i);
String title = jsonPost.getString("title");
Log.v(TAG, "Post" + i +":" + title);
}
}
else{
Log.i(TAG,"UnsuccessfulCode: " + 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;
}
}
}
1 Answer
James Bunn
1,112 PointsSorry...didn't scroll all the way to the right in the logcat view...Please ignore question.