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 Build a Blog Reader Android App Getting Data from the Web Parsing Data Returned in JSON Format

Only showing 10 blogpost not 20!!

When i run the application its only showing 10 blog post instead of 20 like in the video. I'm Using Android Studio for this project. All of my source code is exact as the video tutorial Here is my logcat below. anyhelp would be awesome! Thanks

10-14 12:22:41.529  19863-19880/com.jerdeez.blogreader E/MainListActivity Exception caught:
    org.json.JSONException: No value for posts
            at org.json.JSONObject.get(JSONObject.java:354)
            at org.json.JSONObject.getJSONArray(JSONObject.java:544)
            at com.jerdeez.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:94)
            at com.jerdeez.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:67)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
10-14 12:23:25.329  19939-19939/com.jerdeez.blogreader I/Adreno200-EGLSUB <ConfigWindowMatch:2087>: Format RGBA_8888.
10-14 12:23:25.419  19939-19939/com.jerdeez.blogreader I/Choreographer Skipped 32 frames!  The application may be doing too much work on its main thread.
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity ok
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 0: Cutting-Edge CSS Features You Can Use Today
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 1: After Just 6 Months Learning Nick is a full-time Web Developer
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 2: Making a Network Request in Swift
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 3: New Course: WordPress Theme Development
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 4: How to Install Node.js and NPM on a Mac
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 5: Device Mockups
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 6: Tips to Get Your Child Coding
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 7: Fun at the Treehouse Company Meet-up 2014
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 8: New Course: Moving to WordPress.org
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader V/MainListActivity posts 9: How to Launch a Mobile App
10-14 12:23:26.149  19939-19954/com.jerdeez.blogreader I/MainListActivity Code: 200
10-14 12:24:35.969  19939-19939/com.jerdeez.blogreader W/IInputConnectionWrapper getSelectedText on inactive InputConnection
10-14 12:24:35.969  19939-19939/com.jerdeez.blogreader W/IInputConnectionWrapper setComposingText on inactive InputConnection
10-14 12:45:46.489  19939-19939/com.jerdeez.blogreader E/SpannableStringBuilder SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-14 12:45:46.489  19939-19939/com.jerdeez.blogreader E/SpannableStringBuilder SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

Here 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.util.Log;
import android.view.Menu;
import android.widget.Toast;

import org.json.JSONArray;
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();


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

        private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {

            @Override
            protected String doInBackground(Object[] objects) {
                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();

                  //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
                        JSONObject jSonResponse = new JSONObject(responseData);
                        String status = jSonResponse.getString("status");
                        Log.v(TAG, status);

                        //Creat's an array so we can store are blog posts
                        JSONArray jSonPost = jSonResponse.getJSONArray("posts");
                        //A for loop so are post can run simitanously untill conditions are met.
                        for (int i =0; i < jSonPost.length(); i++){
                            JSONObject jsonPost = jSonPost.getJSONObject(i);
                            String title = jsonPost.getString("title");
                            Log.v(TAG, "posts " + i + ": " + title);

                        }



                    }
                    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 "code: " + responseCode;
            }


        }


        }

2 Answers

URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS); This should work for sure.

Afik,

Thanks you are awesome!!! It is working now. I had a space between symbols (count =")

as shown below.

Before with space
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count =" + NUMBER_OF_POSTS);
No space
("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
New logcat 

10-14 18:32:44.959    2901-2911/com.jerdeez.blogreader V/MainListActivity ok
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 0: Responsive Charts
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 1: Cutting-Edge CSS Features You Can Use Today
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 2: After Just 6 Months Learning Nick is a full-time Web Developer
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 3: Making a Network Request in Swift
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 4: New Course: WordPress Theme Development
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 5: How to Install Node.js and NPM on a Mac
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 6: Device Mockups
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 7: Tips to Get Your Child Coding
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 8: Fun at the Treehouse Company Meet-up 2014
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 9: New Course: Moving to WordPress.org
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 10: How to Launch a Mobile App
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 11: Why We Decided to Take a Paycut
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 12: Interview: Type Designer Mattox Shuler
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 13: How to Install Laravel Homestead on Windows
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 14: Do You Have a Favorite Treehouse Teacher?
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 15: CSS Triggers
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 16: New Course: Object-Orientated Python
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 17: New Course: Publishing an Android App
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 18: Learning Swift vs Objective-C
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader V/MainListActivity posts 19: New Course: Modular CSS with Sass
10-14 18:32:44.969    2901-2911/com.jerdeez.blogreader I/MainListActivity Code: 200

Your URL is wrong kind of you have missed the "="... try

new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" //add the rest here

Gloria

I added the symbols =" but still only showing 0-9.

Thanks

Does it give you the same error?