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 Getting Data from the Web Trying Code and Catching Exceptions

??How DO I DO THIS??

I have no clue how to solve the first objective in this exercise please help me!

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Vikram

I have posted the code below for all the 3 challenges however I would encourage you to try an use the text below first and try to figure it our for yourself first.

Challenge 1

Declaring a public static final String variable, as this challenge is asking you to declare a variable that you wish to remain constant you should use the keywords public,static and final. afterwards you should use the declare the variable the same way you would any other, however as this is a constant the variable should be placed inside the class not the method. So make sure you place it below the class declaration but before the method call to onCreate().

Challenge 2

This challenge should be done inside the onCreate() method.

Okay so we need a try/catch block here, this code tells java we know this might cause an error and if it does we want to do this. inside the try catch block we simply construct a new object using the URL class we can do this by using the standard constructor new URL(), this new object takes a parameter (a URL address), this is the URL constant we declared in challenge 1 so pass this into the URL() constructor.

Challenge 3

All the hard work has been done now!

We only need to add a simple Log.e() method into the catch block we made in challenge 2 using the parameters as described in the challenge description.

I hope this helps but if you are still struggling, like I said the full code is listed below

package com.example;

import android.os.Bundle;
import android.view.View;
import java.net.MalformedURLException;
import android.util.Log;

public class MainListActivity extends ListActivity {

    public static final String URL = "teamtreehouse.com";

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

        // Add Task 2 code here
      try{
        URL treehouseUrl = new URL(URL);
      }catch(MalformedURLException e){
        Log.e("CodeChallenge","This is an error!",e);
      }

    }
}