Hacker News new | ask | show | jobs
by spacemanaki 4747 days ago
Which API were you using? Java's HttpUrlConnection is notoriously badly designed [0] On Android (and elsewhere of course) you can use Apache's HttpClient instead which is a bit less horrible [1]. Part of this problem is checked vs unchecked exceptions, and some of the older APIs in Java use checked exceptions too heavily, IMHO. On top of that, I think all the Java IO stuff provides a pretty leaky abstraction over the system calls.

I had a somewhat long discussion with a colleague about this over the past year, and we looked at all the error cases in the C standard library and came to the conclusion the Java API was probably throwing checked exceptions in one or two corner cases where it should throw unchecked runtime exceptions. I'd be curious what other people think about this. I think specifically, I was asking him (a more Sr. engineer who has used Java heavily and considers it basically good) why closing an IO stream in a finally block throws another checked exception.

In short, I don't think you're doing anything wrong.

[0] See this, but I should caveat this with saying it's a bad API as designed to be consumed by an application. http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLCon...

[1] http://developer.android.com/reference/org/apache/http/clien...

3 comments

I started using HttpClient, but I saw a blog post that said that HttpURLConnection should be used by all new apps, so I went with that. I can't tell you about checked vs unchecked exceptions, as I just found out about them a minute ago.

In the end, I used this excellent library: http://loopj.com/android-async-http/

It's asynchronous, but it still required the work to be done in an AsyncTask, which is odd to me (why spawn a thread to spawn another thread?), but it wouldn't work in the main thread, and Android was good enough to tell me about it.

I'm glad that I'm not doing anything wrong, but I'm also sad because I was hoping I was doing something wrong. Thanks for your reply!

The Android team has dropped support for Apache HttpClient. The party line is that if you are targeting Gingerbread or newer (and you should be), then use HttpURLConnection.

http://android-developers.blogspot.com/2011/09/androids-http...

> I'd be curious what other people think about this.

I think that checked exceptions are a failed experiment in language design. They basically force you to leak abstractions all over your code, reintroducing a lot of the pain of error codes.

You don't need to leak abstractions on exceptions. Libraries should wrap the exceptions and provide meaningful exceptions, not dependent on the exact implementation. Example:

  public String setCache(String key, String value) throws CacheUnavailableException,CacheWriteException {
    try {
    ...
    } catch (SocketException e) {
        throw new CacheUnavailableException(e);
    } catch (SomeMemcacheException e) {
        throw new CacheWriteException(e);
    }
  }
This will allow library clients to catch exceptions that mean something to them, not exceptions that only make sense if you know how the library is using memcache.