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

Zachary Barnett
Zachary Barnett
1,978 Points

BlogReader app - JSON exception

I'm close to the end of the BlogReader app project. Running the code on the emulator always worked fine.

I try it out on my android device, and it worked one time, in the middle of my testing (one random success after 10+ tries) but I keep getting these two errors... how do you fix them?

org.json.JSONException: Expected ':' after (diamond characters)

org.json.JSONException: Unterminated string at character 6411 of { (continued list of json data....)

Perhaps these issues are addressed in future videos/lessons - if they are, please let me know.

Any ideas from the experienced?

10 Answers

i had this problem too!

But, if you download the blogreader app on google play store, the app does not work sometimes..

it just randomly works..

It could be because your json page does not return a content-length.

    StringBuilder builder = new StringBuilder();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("your url");
            try {
                HttpResponse response = client.execute(httpget);
                StatusLine statusLine = response.getStatusLine();
                respCode = statusLine.getStatusCode();
                if (respCode == HttpURLConnection.HTTP_OK) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                    String line;
                    while((line = reader.readLine()) != null){
                        builder.append(line);
                    }
                }
                    else{
                        Log.e(TAG, "Failed to download");

                    }

try this, it's not using a Char and the content-length to store the request in a string. It takes the stream and appends every bit to the string :)

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi everyone,

This is an interesting bug. I have not been able to reproduce it myself. I occasionally have gotten a timeout error, but not a JSONException. I will take a further look, but if anyone experiences this, please try to capture the JSON being returned or at least note the time and date of the request so I can look at the blog API.

If we need to add a video to address an issue we can certainly to that!

@Fabian, I actually had a version of the Blog Reader with very similar code but thought it was easier to explain how the code I ended up using works. Thanks for posting it in here!

Jisan Zaman
Jisan Zaman
3,363 Points

Hi Ben,

I get this error:

E/MainListActivity﹕ Exception caught : org.json.JSONException: Unterminated string at character 2832 of {"status":"ok","count":10,"count_total":1922,"pages":193,"posts":[{"id":24582,"url":"http:\/\/blog.teamtreehouse.com\/holiday-cheer-almost","title":"Holiday Cheer is Almost Here!","date":"2014-12-19 09:01:43","author":"Faye Bridge","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/12\/day1-sticker-pack-150x150.png"},{"id":24532,"url":"http:\/\/blog.teamtreehouse.com\/optimize-images-web","title":"Optimize Images for the Web","date":"2014-12-18 09:00:57","author":"Nick Pettit","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/12\/svg-example-150x150.png"},{"id":24540,"url":"http:\/\/blog.teamtreehouse.com\/new-courses-2","title":"New Courses: Java Objects & SVG Basics","date":"2014-12-17 09:39:12","author":"Gill Carson","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/12\/java-150x150.jpg"},{"id":24553,"url":"http:\/\/blog.teamtreehouse.com\/material-ui-react","title":"Material UI in React","date":"2014-12-16 12:11:23","author":"Jason Seifer","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/12\/Perf.Rocks_-150x150.png"},{"id":24544,"url":"http:\/\/blog.teamtreehouse.com\/history-of-treehouse","title":"Ryan Talks About the History of Treehouse","date":"2014-12-16 11:30:52","author":"Gill Carson","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2014\/12\/executive-hang-150x150.jpg"},{"id":24534,"url":"http:\/\/blog.teamtreehouse.com\/java-and-everyday-objects","title":"Java & Everyday Objects","date":"2014�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

As you can see, after a while the data turns into question marks. I imagine that probably has something to do with the error: but any help with this would be appreciated...

Zachary Barnett
Zachary Barnett
1,978 Points

Perhaps these issues are addressed in future videos/lessons - if they are, please let me know.

@Ben The Problem is that some servers don't send the content.length in their header. the Treehouse server sends it.

For example the "API" of my website (just as an example, to see the server answer)

http://homeworkcloud.me/appmanage.php?tag=login&email=test&password=lol

If you look at the header you will see that the server transer-encoding is "chunked", but there is no "content-length" sent.

If you look at the header from the URL you used in the tutorial, you will see that a content-length is sent!

So the problem is that in the tutorial we use

a char with the length of the content. But, because the server does not send any content length, the char is null and no data can be stored in it

The version I posted above, no content-length is needed, the Data is getting store until there is something to store, then it automatically ends.

(I got the solution by googling a lot, not by myself)

If I got anything wrong, please correct me, I am aandroid programming newbie :)

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi @Fabian,

I missed your post somehow, but you have posted an excellent solution! I just tried it out and verified it works. The offending post from Treehouse is from 2/5, and the problem character appears to be the vertical pipe:

"jQuery 1.9 | Treehouse Show Ep 26"

Here's the strange part I haven't figured out, though. Someone else came across this problem and I dug deeper tonight since I can now reproduce it. You can read my notes in that post, but basically the code from the videos breaks on certain special characters, but it works if I am running in debug mode. I'll do more research tomorrow. I'll let you know how it all works out, but thanks for chipping in with your research and solution!

I'm not convinced that it has to do with the Content-Length, though. I took a look at the link you posted in Chrome developer tools (in the Network tab) and it does return a Content-Length:

HTTP/1.1 200 OK
Date: Thu, 11 Apr 2013 02:16:20 GMT
Server: Apache
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 85
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: application/json
Vivek Marakana
Vivek Marakana
16,412 Points

@Ben i have done some modifications to the app. And in order to that i have produced my own WordPress plugin with custom features.

This plugin sets content-length header even if data is chucked.. In fact i am setting custom header called Length when Content-Length is not supported. So my HTTP Response look like this :

Accept-Ranges:none
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:application/json; charset=UTF-8
Date:Thu, 11 Apr 2013 08:16:22 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Length:639
Pragma:no-cache
Server:Apache
Transfer-Encoding:chunked
Vary:Accept-Encoding,User-Agent
X-Pingback:http://htmlcsstutor.com/xmlrpc.php
X-UA-Compatible:IE=Edge,chrome=1

And i have also implemented offline reading and offline post-list to read when offline. How cool is that :)

If possible check-out my app at http://demo.htmlcsstutor.com/blogreaderapp/HTMLCSSTutor.apk

If you like the app then i can share the creation of it with other users.

Thanks.

Yes, Vivek Marakana I would highly appreciate if you could share the creation of your app, so that we can also learn the new techniques like refreshing, offline html data saving. So please share...

@Ben

Response Headersview source
Connection:Keep-Alive
Content-Type:application/json
Date:Thu, 11 Apr 2013 10:04:14 GMT
Keep-Alive:timeout=15, max=100
Proxy-Connection:Keep-Alive
Server:Apache
Transfer-Encoding:chunked
Via:1.1 HAK1MPS
X-Powered-By:PHP/5.3.3-7+squeeze14

that's my header.. there is no content-length.. that's really weird..

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I added some comments to the other post about this issue. Please check it out. As for the original question in here, I need to figure out how best to address this in the context of the project. I'm thinking maybe an extra video at the end of the stage or whole project that talks about this problem and how it was discovered, researched, and solved, as it's very representative of real-world development! Thanks, everyone!

Side notes:

@Fabian, when I debug and log from the app I don't get the Content-Length property. I guess Chrome is requesting it differently. :)

@Vivek, thanks for your input! I wrote more to you in the other post.

Vivek Marakana
Vivek Marakana
16,412 Points

@Fabian this is because response is chunked Transfer-Encoding:chunked..

In my header there is no Content-Length header but i have added my custom header called Length.. see it..

This is done using my own edited plugin for more detail see my other comment

@Vivek

I think that's a great plugin, but I think it's not the right way to handle this error, because what if you want to read from a stream, Like, for example, Facebook (that did not work for me), where you cannot modify the header..

So I think the right solution for this whole problem is to work with Stringbuilder, as in my example :)

@Ben

I really appreciate your effort! That's why I love your courses ! :)