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
Sjoerd van den Belt
6,692 PointsUnknown exception caught in Moving Work from the Main Thread to an AsyncTask
Hi everyone, I've finished the Moving Work from the Main Thread to an AsyncTask but when I try to compile it in eclipse it throws me an exception:
11-17 15:16:29.281: E/MainListActivity(870): java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
11-17 15:16:29.281: E/MainListActivity(870): at com.SjoerdvdBelt.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:46)
11-17 15:16:29.281: E/MainListActivity(870): at com.SjoerdvdBelt.blogreader.MainListActivity$GetBlogPostsTask.doInBackground(MainListActivity.java:1)
11-17 15:16:29.281: E/MainListActivity(870): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-17 15:16:29.281: E/MainListActivity(870): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-17 15:16:29.281: E/MainListActivity(870): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-17 15:16:29.281: E/MainListActivity(870): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-17 15:16:29.281: E/MainListActivity(870): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-17 15:16:29.281: E/MainListActivity(870): at java.lang.Thread.run(Thread.java:841)
My code in eclipse is:
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);
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
//Toast.makeText(this, getString(R.string.no_items), Toast.LENGTH_LONG).show();
}
@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;
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, String>{
@Override
protected String doInBackground(Object... arg0) {
int responceCode = -1;
try {
URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
HttpsURLConnection connection = (HttpsURLConnection) blogFeedUrl.openConnection();
connection.connect();
responceCode = connection.getResponseCode();
Log.i(TAG, "Code: " + responceCode);
}
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: " + responceCode;
}
}
}
Thanks, Sjoerd
2 Answers
Ben Jakuben
Treehouse TeacherLet's take a look at the first line from the error you posted:
java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
That tells us that HttpsURLConnection (note the s!) isn't correct for this situation. Try it without the "s". :) Also note the URL we are using; it's just "http://..."
Sjoerd van den Belt
6,692 PointsIt worked! Thanks a lot :)