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 Build a Blog Reader Android App Getting Data from the Web Getting JSON Data from an HTTP Request

HTTP_OK does not ring any bell to my httpURLconnection method

HTTP_OK does not ring any bell to my httpURLconnection method

16 Answers

This is strange!

I'm wondering if you've got the wrong file imported. In your import list, do you have:

import java.net.HttpURLConnection;

EDIT - try deleting that line, then reimporting it by clicking inside the HttpURLConnection word and pressing the relevant buttons.

yep. thanks for the help

Hi Jerome,

Can you post your line of code please?

The code should look like:

if (responseCode == HttpURLConnection.HTTP_OK) {

The underscore is the only difference I can guess at from your question.

Steve.

sorry actually it does look like exactly that:

if(responseCode == HttpURLConnection.HTTP_OK){

seems that diffenrence is from the capital letter in Response code. But then i dont know what i should do with it.

No - that was me being wrong ... edited!!

Odd ... can you post the code in the doInBackground method.

Thanks.

protected JSONObject  doInBackground(Object...arg0) {

            int responseCode = -1;
            JSONObject jsonResponse= null;

            try{
                URL blogFeedUrL = new URL("http://www.hashtag-whistleblower.com/category/politics/?json=1" + NUMBER_OF_POSTS);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrL.openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();

                if(ResponseCode == HttpURLConnection.HTTP_OK){
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);
                    int contentLength = connection.getContentLength();
                    char[] charArray = new char [contentLength];
                    reader.read(charArray);
                    String responseData = new String(charArray);
                     jsonResponse = new JSONObject(responseData);

                }else{

                    Log.i(TAG,"UNsuccessful HTTP Response Code: " + responseCode);


                }
                Log.i(TAG,"code:"+responseCode);
            }
            catch(MalformedURLException e){
                logExeption(e);

            }
            catch( IOException e){

                logExeption(e);
            }
            catch (Exception e){
                logExeption(e);
            }
            return jsonResponse;
        }

All seems fine there, except for the capital R which was probably my fault!!

Does this give you a compiler error at the if statement? What does that say?

If you start typing HttpURLConnection. do you get a list of options to select from?

the compiler gives me "rename the reference" for: HTTP_OK

i mostly have: get() methods

but not HTTP_OK

Yes, as I type the [dot] I get getDefaultAllowUserInteration() and getFileNameMap() etc.

But as soon as I type a capital H, I get a list of HTTP ints. What do you get?

capital H is red and no propositions from the compiler

when i start the app i get : Error:(123, 53) cannot find symbol variable HTTP_OK

well i actually do : import java.net.HttpURLConnection;

Try deleing it, save, then see if the reimporting helps clear the error.

Try a project clean, and also try File | Invalidate Caches / Restart

That might help!

i guess i might have to.

Should only take a couple of minutes. Delete the import line first, then fix the error with autocomplete.

If that doesn't work, clean the project then do the Invalidate Caches / Restart thing.

worked with: java.net.HttpURLConnection.HTTP_OK rather than HttpURLConnection.HTTP_OK

Excellent!

We got there in the end!