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 trialtshuutheniemvula
7,251 PointsStage3: I don't get 200 Http response, just the int variable declared. All permissions present. What am I doing wrong?
Here is my Java:
public class MainListActivity extends ListActivity {
protected String[] mBlogPostTitles;
public static final int NUMBER_OF_POSTS = 20;
public static final String TAG = MainListActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
if (isNetworkAvailable()) {
GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
getBlogPostTask.execute();
}
else {
Toast.makeText(this, "Network is Unavailable", Toast.LENGTH_LONG).show();
}
//Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private class GetBlogPostTask extends AsyncTask<Object, Void, String> {
@Override
protected String doInBackground(Object[] objects) {
int responseCode = -1;
try {
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
Log.i(TAG, "Code:" + responseCode);
}
catch(MalformedURLException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (IOException e){
Log.e(TAG, "Exception caught: ", e);
}
catch (Exception e){
Log.e(TAG, "Exception caught: ", e);
}
return "Code:" + responseCode;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
It runs fine, returns no exceptions but I don't get a 200 Http response just the -1 variable declared.
2 Answers
Josh Guenther
6,042 PointsI believe you are simply missing this line of code:
responseCode = connection.getResponseCode();
You just have to get the response code and assign it to your responseCode variable. Put it right where you are currently logging your response code.
tshuutheniemvula
7,251 PointsOh thanks! Makes me more wary double checking variables before compiling.