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

Problem in the getContentLength() function

I thought i'd make another post for this, but this question stems from the converting a website into JSON thread.

After following all the steps Ben told me, installing the plugin and implementing the function in the core.php, I was able to convert my website into a similar JSON view, Now it displays correctly when I type : http://codingmash.com/api/get_recent_summary/?count=20

However, my application doesn't work. After debugging, I've found out that in the doInBackground() method, it establishes an ok HTTP connection, and thus goes into the try block. Here, it throws an exception at the line:

char [] chararray = new char [contentLength];

In the debugger, I found out that the contentLength variable was -1 after using the getContentLength(). And this was probably the reason the exception occurs. But I have no idea why it returns -1. All I got what this function Returns the content length in bytes specified by the response header field content-length or -1 if this field is not set. But I dont know what a response header field is.

Can anyone help? :(

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Hamza Tahir,

I'm sorry you are having this trouble! It looks like you came across an issue with the Content Length property that was logged in this other forum post. Please read through that post as I posted an alternative version of the doInBackground that should fix your problem.

I would ultimately like to re-record that video to address this issue, but until then I've added some notes about it in the "Teacher Notes" section below the video. I know that's not the best solution as not everyone knows those notes are there (or view them), but hopefully this other solution helps you out!

Great, thanks for the quick response Ben! Yes, its working now, but when I click on a blog article, it doesn't load properly on the WebView. Is that a wordpress theme problem?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you debug or log the exact URL that's being used to load the WebView? Maybe there's an issue getting it or how it's being formatted, either by the app or by WordPress.

Ill choose one URL. http://codingmash.com/c/the-substring-finding-algorithm/. All of the links behave this way. What actual details and values should I be looking at when I debug it?

I know that it enters the BlogWebView actity fine, and then runs okay, and the mURL is correct. Then when I keep on debugging , it eventually stops at something called 'Looper.class', and then it doesnt debug further. In the app, some features load(Like the comment section and the author name and search etc). But the actual article doesnt load, along with a few other features

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Strange. What device or emulator are you running it on? I just tried commenting out the webView.loadURL() method in my code and replacing it with the following, and it worked fine:

webView.loadUrl("http://codingmash.com/c/the-substring-finding-algorithm/");

The only issue was that the web page itself doesn't scale to a mobile device. I ran mine on a Nexus 7. Can you take a screenshot and post it in here? If you need, you can upload it to a site like imgur and then use the Markdown Cheatsheet on how to include images.

Im using two devices. A virtual device, which is Nexus 7 same as urs , and my own android device which is 2.5 Gingerbread.

Yes, that's the problem. The webpage doesnt scale to an android device. What to do about that? Andd did everything from the actual website load on your emulator? Because mine shows a big white screen in the middle. Here are the screen shots:

Actual page: alt text

App page showing: alt text

Could the problem be in the theme I use on wordpress? If so, how to check

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Ah, gotcha. Yes, I get the same thing in my emulator (see below). This does appear to be a problem with how the site is being rendered for the Android browser. You can try posting a new question in the forum here in the WordPress category, but I'm afraid I can't be of much more help myself!

Gingerbread screenshot

:( okay. But thanks for your time anyway! Looking forward to the advanced android app!