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

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Weekend Fun: Latest Android stage "Getting Data from the Web" is now live!

Hey everyone! Just in time for Groundhog Day, the latest stage in the Build a Blog Reader App project is live!

http://teamtreehouse.com/library/android-development/build-a-blog-reader-app/getting-data-from-the-web

Now that we're comfortable displaying simple data in a ListView, let's take a look at how to connect to a website and request some data. This stage will cover how to establish that kind of connection and then use an AsyncTask in a separate thread of execution to retrieve data. We'll also see how to parse through the JSON data returned from the blog, and along the way we'll cover Java Exceptions and how to detect and handle them.

Let me know if you have any questions, problems, or feedback. Happy coding!

Ben

12 Answers

+1 just going through it now.

Just a note, going through this, almost done.

Had a problem with the quiz:

http://teamtreehouse.com/library/getting-json-data-from-an-http-request

code: String responseData = "";

try { URL treehouseUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=20"); HttpURLConnection connection = (HttpURLConnection) treehouseUrl.openConnection(); connection.connect(); int responseCode = connection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {

    InputStream inputStream = connection.getInputStream();    
  Reader reader = new InputStreamReader(inputStream);
  char[] charArray = new char[connection.getContentLength()];
  reader.read(charArray);
  String responseData = new String(charArray);

Gives me the error: Bummer! Compilation Error! Make sure you are setting 'responseData' correctly inside the 'if' block using 'new String()' with 'charArray' as the parameter.

So, I know it's because the variable is already declared for me, but it threw me for awhile at the beginning because the error message didn't mention about the variable being declared multiple times in the same scope. If it's not too terribly difficult it might be worth modifying that to give a more meaningful message.

Ultimately the compiler is there to catch syntactical errors like this, given that it's probably not as simple as all that with the environment you guys have built.

I do have to say, overall it's pretty darned impressive.

Gary

Another one, but this one I can't figure out:

// JSONObject 'jsonData' was loaded from data.json

String name = jsonData.getString("name"); String publisher = jsonData.getString("publisher"); String language = jsonData.getString("language");

JSONArray jsonBooks = jsonData.getJSONArray("books");

for (int i = 0; i < jsonBooks.length(); i++) { String title = jsonBooks.getJSONObject(i).getString("title"); String pages = jsonBooks.getJSONObject(i).getString("pages");

Log.i("CodeChallenge", title + ", " + pages); }

As far as I can tell that is correct, the error message is: "Bummer! Make sure you write each book to the log exactly as specified in the instructions."

yeah im having the same issue...

// JSONObject 'jsonData' was loaded from data.json

String name = jsonData.getString("name"); String publisher = jsonData.getString("publisher"); String language = jsonData.getString("language");

JSONArray jsonBooks = jsonData.getJSONArray("books");

for(int i = 0; i < jsonBooks.length(); i++) { JSONObject jsonBook = jsonBooks.getJSONObject(i); Log.i("CodeChallenge", jsonBook.getString("title") + ", " + jsonBook.getString("pages")); }

getting the error:

Bummer! Make sure you write each book to the log exactly as specified in the instructions.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

@Gary, thanks for the awesome feedback! Glad you got through this long stage this weekend! For your first point, I got that same feedback about 'responseData' elsewhere, so I added a hint to the compilation error that states that 'responseData' is declared on line 1. You are right that catching the compiler errors and outputting them is not straightforward in our custom code challenge engine, but that's something we've talked about for multiple languages and will try to implement soon.

For your second question...for a while I thought you found an error in the challenge, but I found a problem with your code. It's another case where having compiler messages would have made it easy for you to fix! Here's the hint: check the "pages" element in data.json. It is not a String. :smiley: I might add a hint to that compiler message, too.

Thanks again for your feedback!

Oh wow. That was a tricky one.

f**k

Got an idea, you could express these tests as downloadable projects. Even public github repos.

So if someone runs into a problem they could just pull the appropriate repo and then be able to diagnose the problem.

I considered doing that myself locally, but I decided to spend time with the wife instead.

The actual skill involved in pulling a repository off of github is trivial.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hmmmm...I'd have to think about that and talk to some of our developers. We don't want to open-source parts of our engine for IP reasons, and I stub out a lot of the classes by extending the base classes so I can take a deeper look into the code during execution. It would be helpful to have files you could look at in an IDE, because I definitely do that sometimes when writing the challenges. :smile:

P.S. Thanks for all the feedback and good questions! Glad to have you on board and working through the Android stuff!

Well I can see the open sourcing issue, I'm not sure how I feel about that. But then I don't have a stake in it either. I keep the stuff that's directly relevant to my current employment in a private repo as much as I'd prefer to open source everything. However in my case I dont' think it's that valuable... In your case... A very, very complex discussion...

However that wasn't what I was referring too. If you took your code challenge project, IE in this case a simple project that JUST loads the JSONObject. And made that fetchable on github you'd end up with something like:

PROJ_NAME:

  • src ** com.teamtreehouse.codechallenge.proj_[X]part[Y] *** scaffolding.java <-- contains code setup *** work.java <-- contains code snippet for student to work on.
  • res: ( Any resources needed) ** JSONBooks.json (in this case).

Essentially if a student runs into trouble they can pull the repo, execute the code, see what their doing wrong...

I really have no idea if this would be useful for anyone but myself, you'd probably have to do it and then see how many times it gets pulled down.

We are talking about pretty small snippets of code that you've probably already got though... So total investment should be quite low. Also, could be a good way to get new subscribers, IE people googling through github and coming across compilable snippets that illustrate important techniques.

If I have time this evening when I get home from my Saturday spent at work :( I'll do what I have in mind myself and push it to a public github repo and link it here. I'm hoping there's no issues with that :)

G

Got about 1/2 way through on this. Running into weird problems reading res/raw/ text files in a test project.

Hopefully figure it out this evening and push it to github when I'm done.

https://github.com/luck02/AndroidTestingScaffolding

That's the general idea. I didn't actually finish it, but just generally sketched out the idea.

IE here's an android TestCase, go make it pass.

Also a good way of introducing TDD

It was also a great way for me to introduce myself to the Android test classes.