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

Cant get Code Challenge to work

Filling our String array and Creating an Adapter code challenge 1/3

Is something wrong with this code?

JSONArray jsonVideos = jsonData.getJSONArray("videos");
String[] titles = new String[jsonVideos.length()];
for(int i =0; i <jsonVideos.length() ;i++)
{
    JSONObject post = jsonVideos.getJSONObject(i);
    String vtitle =post.getString("title");
    vtitle = Html.fromHtml(vtitle).toString();
    titles[i]= vtitle;
}

5 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

You're too good! The Code Challenge isn't expecting the Html.fromHtml() call. We have a few tricks in place to do some of our validation. Just remove that line and you're good to go. I'll see about fixing the Code Challenge engine to account for what you did, which makes sense.

Hi Ben,

Thanks for your response on that one, but I had a similar issue with another code challenge as well. Parsing Data returned in JSONFormat. task 3/3 any combination of comma and space doesnt work. "," +" " OR ", "

The code im using is as follows: 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 post = jsonBooks.getJSONObject(i); String title =post.getString("title"); String pages =post.getString("pages"); Log.i("CodeChallenge", title + " , " + pages); }

Ben Jakuben
Ben Jakuben
Treehouse Teacher

This one is intentionally tricky. "pages" is an integer, so you need to use a different method than "getString". It trips a lot of people up, though, so I will likely change it.

So, what am I doing wrong on this one? BTW, thanks for the hint about the integer but

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);
    String title = jsonBook.getString("title");
    int pages = jsonResponse.getInt("pages");
        Log.i("CodeChallenge", title + ", " + pages);
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sorry for the late reply! In this one you had jsonResponse trying to get the pages. Looks like you fixed that below...

replaced the jsonResponse with jsonBook but still not it...

// JSONObject 'jsonData' was loaded from data.json
String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language = jsonData.getString("language");

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

for (int i = 0; i< jsonBooks.length(); i++) {
    JSONObject jsonBook = jsonBooks.getJSONObject(i);
    String title = jsonBook.getString("title");
    int pages = jsonBook.getInt("pages");
  Log.i("CodeChallenge", title + ", " + pages);
}
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Again, sorry for the late response! Hopefully you got past this already. :) In this latest code snippet you are declaring int i twice, which should give a compiler error as it can only be declared one time.