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 Cleaning Up Our Presentation

Getting exception please help!!!

the progress bar is working properly but after loading it shows no items to display and there is no blog .....my emulator is not in airplane mode i am pasting my code please check if there is problem package com.example.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.JSONException; import org.json.JSONObject;

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.util.Log; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; 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 ;
protected ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);

    mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);


    if (isNetworkAvailable()){
        mProgressBar.setVisibility(View.VISIBLE);
        GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
        getBlogPostTask.execute();
    }else {
        Toast.makeText(this, "Network is unavailable", Toast.LENGTH_LONG).show();
         TextView emptyTextView = (TextView) getListView().getEmptyView();
         emptyTextView.setText(getString(R.string.no_items));
    }


}


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 handleBlogResponse() {
    mProgressBar.setVisibility(View.INVISIBLE);
    if (mBlogData == null){
        updateDisplayForError();
    }
    else {
        try {
            Log.d(TAG,mBlogData.toString(2));
        } catch (JSONException e) {
            Log.e(TAG,"Exception Caught !! ", e );
        }
    }
}


private void updateDisplayForError() {
    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();

    TextView emptyTextview = (TextView) getListView().getEmptyView();
    emptyTextview.setText(getString(R.string.no_items));
}

private class GetBlogPostTask extends AsyncTask<Object, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(Object... params) { 
        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;
    }

    @Override
    protected void onPostExecute (JSONObject result ){
        mBlogData = result;
        handleBlogResponse ();

    }

}

}

7 Answers

Found It there was an error in the url and the eclipse ide was not working properly

Did you look in the logcat tab? Maybe there is a more specific reason as to why there is no data.

Can you please post the stack trace of the uncaught exception.

whats stack trace.......... and how i will find which exception is uncaught now its an malformed url exception and some times its something else .......!!

I guess I found the error

Pay attention to this line

URL blogFeedUrl = new URL ("<http://blog.teamtreehouse.com/api/get_recent_summary/?count= + NUMBER_OF_POSTS>");

1) Remove the < and > sign from the url. JSON will not work well if you have any other special characters in your URL like <, >, \ , @ , & , # etc.

2) Second NUMBER_OF_POSTS is not a string but a final int variable that you have defined in the beginning. Take it out of the " " and concatenate + with the URL.

So what it should be instead is

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

When you do get an error like this first make sure to check the logcat or the console and locate the line where error occurred.

Hope it helps.

now its an please help how to solve this exception please .....!!

07-02 14:16:26.035: E/MainLIstActivity(1313): org.json.JSONException: Unterminated string at character 5889 of {"status":"ok","count":20,"count_total":1774,"pages":89,"posts":[{"id":23756,"url":"http:\/\/blog.teamtreehouse.com\/build-a-game-with-sprite-kit","title":"New Course: Build a Game with Sprite Kit","date":"2014-07-02 13:52:46","author":"Gill Carson","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/07\/Space-Cat-Preview-150x150.png"},{"id":23791,"url":"http:\/\/blog.teamtreehouse.com\/treehouse-teachers-real-photoshoot","title":"Treehouse Teachers – The Real Photoshoot","date":"2014-07-01 18:28:06","author":"Gill Carson","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/07\/teachers_1-150x150.jpg"},{"id":23787,"url":"http:\/\/blog.teamtreehouse.com\/data-binding-techniques","title":"Data Binding Techniques","date":"2014-07-01 16:53:31","author":"Jason Seifer","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/07\/Data-binding-Techniques-CSS-Techniques-Zepto.js-The-Treehouse-Show-Episode-97-YouTube-2014-07-01-16-48-55-2014-07-01-16-51-20-1???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Still an error.....!!

And what does it say now in the console or the logcat view?

that same Json exception

Can you see what line is the error coming from and paste that line here?