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 trialjerome sinpaseuth
2,632 PointsHTTP_OK does not ring any bell to my httpURLconnection method
HTTP_OK does not ring any bell to my httpURLconnection method
16 Answers
Steve Hunter
57,712 PointsThis 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.
jerome sinpaseuth
2,632 Pointsyep. thanks for the help
Steve Hunter
57,712 PointsHi 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.
jerome sinpaseuth
2,632 Pointssorry actually it does look like exactly that:
if(responseCode == HttpURLConnection.HTTP_OK){
jerome sinpaseuth
2,632 Pointsseems that diffenrence is from the capital letter in Response code. But then i dont know what i should do with it.
Steve Hunter
57,712 PointsNo - that was me being wrong ... edited!!
Steve Hunter
57,712 PointsOdd ... can you post the code in the doInBackground
method.
Thanks.
jerome sinpaseuth
2,632 Pointsprotected 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;
}
Steve Hunter
57,712 PointsAll 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?
Steve Hunter
57,712 PointsIf you start typing HttpURLConnection.
do you get a list of options to select from?
jerome sinpaseuth
2,632 Pointsthe compiler gives me "rename the reference" for: HTTP_OK
jerome sinpaseuth
2,632 Pointsi mostly have: get() methods
but not HTTP_OK
Steve Hunter
57,712 PointsYes, 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?
jerome sinpaseuth
2,632 Pointscapital H is red and no propositions from the compiler
jerome sinpaseuth
2,632 Pointswhen i start the app i get : Error:(123, 53) cannot find symbol variable HTTP_OK
jerome sinpaseuth
2,632 Pointswell i actually do : import java.net.HttpURLConnection;
Steve Hunter
57,712 PointsTry 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!
jerome sinpaseuth
2,632 Pointsi guess i might have to.
Steve Hunter
57,712 PointsShould 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.
jerome sinpaseuth
2,632 Pointsworked with: java.net.HttpURLConnection.HTTP_OK rather than HttpURLConnection.HTTP_OK
Steve Hunter
57,712 PointsExcellent!
We got there in the end!