The solutions
- Use HttpURLConnection instead of HttpClient. According to the Android developer's blog, it is the best choice: http://android-developers.blogspot.fr/2011/09/androids-http-clients.html.
- Create an instance of HttpClient in the main thread of your application, as soon as possible, for instance in its onCreate method.
The cause
Some android devices, like the T-Mobile MyTouch Q, use a custom implementation of the Apache HttpClient library. On such devices, creating the first HttpClient of your application not in the main thread will result in an exception being thrown :
java.lang.IllegalArgumentException: androidApplicationContext must be not null!
In my specific case, it was thrown in GbaServiceConnectorSynchronizedSingleton.java, line 76, because of an analytics library.
How to reproduce
Here is a link to a test case allowing to reproduce the issue: https://gist.github.com/Bastoche/6133227