HttpClientBuilder missing on Android?

Oliver Hausler picture Oliver Hausler · Jan 2, 2015 · Viewed 7.1k times · Source

Apache has deprecated DefaultHttpClient, but it seems this is not the case for Android, see also here Deprecated Java HttpClient - How hard can it be?

Importing

org.apache.httpcomponents:httpclient:4.3.5

instead of

new DefaultHttpClient(); 

I would now use

HttpClient httpClient = HttpClientBuilder.create().build();

to create an http client. This works fine in a Java project, but when used in an Android project, the following import is missing

import org.apache.http.impl.client.HttpClientBuilder;

while

HttpClient sendClient =  new DefaultHttpClient();

is not marked as deprecated in Android and compiles fine.

I don't want to add the Apache httpclient a second time (and if I would do so, Android Studio would exclude it anyhow).

The Android documentation says here http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String) to use

AndroidHttpClient.new Instance(string)

to "get an http client with reasonable defaults".

Does anybody know what is the correct way of creating an HttpClient on Android, and what would be the userAgent string!?

Answer

ok2c picture ok2c · Jan 2, 2015

Google Android ships with an extremely outdated (pre-BETA) version of Apache HttpClient 4.0.

If you want to use newer Apache HttpClient APIs with Android you should consider using the official Android port.