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.

JavaScript

Problem with Android parsing JSON code challenge

I'm having problems with the final step of this code challenge. I keep getting a failure with the description: "Make sure you write each book to the log exactly as specified in the instructions."

I'm pretty sure my log format is correct:

jsonBook.getString("title") + ", " + jsonBook.getString("pages")

as per the instructions: "write the book title, a comma, a space, and the number of pages. Ex. output: 'Book title 1, 300'."

I'm also getting an usually high number of "There was a problem connecting to the Code Challenge Engine." errors when I try to check my work. About 3 out of 4 tries return this error.

Has anyone else run into this problem? Did you resolve it? How?

Thanks!

10 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

You're not the first to encounter this! The first error is because the data for number of pages in data.json is not a String, so the getString() method won't work. If this continues to cause trouble for people then I will add a hint to that task. :smile:

As for the "problem connecting", I'll have to take a look at the code challenge engine and follow up with our dev team. Thanks for the feedback!

Hi, Ben. Can you explain to me why the comma in between the plus's has quotes surrounding it?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sure thing! So let's start with the desired final outcome. We want to have the title and page in one String together, separated by a comma:

"HTML5 Foundations, 384"

But to get this, we actually need to concatenate three different Strings:

"HTML5 Foundations" + ", " + "384"

So the first string (the title) comes from the variable that is returned by the getString() method.

The second string (the comma) is set by us, which is why it has double quotes. If we didn't have double quotes we would get a syntax error, because the comma would be looked at by the Java compiler as a special character, and it wouldn't know what to make of it. By adding the quotes we are making it a String, which is the correct syntax.

The third string is a little different. The variable returned by the getInt() method is actually an int. But whenever we concatenate an int with a String, Java automatically converts the int to a String (in essence it just adds double-quotes around the int number).

Hopefully this helps and doesn't just muddy up the water further! Let us know if you have any follow-up questions.

Stefan Bols
Stefan Bols
2,515 Points

Hi Ben. 7 month after this post. I was stuck at this exact same point with the same problem. I used getString() instead of getInt().

I don't think that it's good for anyone that you write a hint in the code challenge that says anything about getInt(). But you might write a hint "Problem executing your code - use the Treehouse Forum to get help, remember to search your problem before asking" - or something like that. Then the user has to work for the answer, instead of getting it served :-)

Just my humble opinion.

  • Stefan
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks, Stefan! It's awesome to see you and others using the Forum like this for help. That's exactly what makes it great. And it's good practice for that type of problem-solving in real world apps. Cheers!

Dan Giles
Dan Giles
2,243 Points

Hi Ben,

If I'm not mistaken, I believe the "getString("pages") will still work in this case, because the JSONObject will coerce the int into a String for the return. According to the JSONObject documentation:

"This class can coerce values to another type when requested.... When the requested type is a String, other non-null values will be coerced using valueOf(Object)"

I just tried it (on the id field from the Treehouse blog) and it seemed to work out.

That said, it is maybe still better to use the getInt("pages") to more clearly specify what data we are working with

Ben Jakuben
Ben Jakuben
Treehouse Teacher

You are correct! Another student recently pointed out this error in our Code Challenge engine. I will update it accordingly. Thanks for chiming in!

Dan Giles
Dan Giles
2,243 Points

Thanks Ben; I'm really enjoying the class BTW, very beneficial.

Thanks! I was in "auto pilot" mode and just used .getString() for all the types from the JSONObject. Problem solved!

The "problem connecting" problem was much better tonight. I didn't see it at all. Could've been a network load issue.

Thanks for the GREAT lessons!

Anton Seidler
Anton Seidler
877 Points

What am I doing wrong?

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

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

@Anton, this is where IDEs really help! I'm sorry for your trouble with this error. Hopefully we can build more robust features like a full-fledged IDE in the future.

Anyhow, your problem was simply a typo: you have "t" and "h" switched in "length".

Anton Seidler
Anton Seidler
877 Points

Oh ... Thank You, Ben! No more coding at 6am. Will also try to check my code with Eclipse next time.

Hi,

Instead of using the getString statement directly within the Log.i method, can't I store it in a variable and then pass it to Log.i?

It shows correct output only if I directly use it in Log.i..

Please help!

Gentlemen, thanks! That was a real challenge. The good new is, that there are resources online that can help. The best resource is the API reference. That was a great project!

Ben Jakuben or anyone else for that matter,

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

Mind letting me know whats wrong with this.

Stefan Bols
Stefan Bols
2,515 Points

Do you have declared your varible "i"?

For ( int i = 0; ....... ?

Best, Stefan

classic mistake, thanks.

Stefan Bols
Stefan Bols
2,515 Points

You're welcome :-)

Rahsan Boykin
Rahsan Boykin
2,067 Points

we really have to figure out how to connect forum questions relating to the same challenge. This is a critical thread.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

That is definitely on our list of priorities. Don't know where it stands right now (might be in development), but I'll check up.

Erin Kabbash
Erin Kabbash
5,210 Points

Agreed, I was stuck on this one for a while. Seeing the getInt call instead of getString in this thread helped a lot. Good lesson learned for the future I guess! :)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I thought I fixed that! getString() should actually work as well. I tried it in Eclipse a while back and was surprised to see it work and updated the code challenge. I'll have to take another look.

Either way, glad you got it! :)