且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HttpClientBuilder缺少在Android?

更新时间:2022-12-16 16:57:48

谷歌Android附带了一个非常过时的(pre-BETA)版本的Apache的HttpClient 4.0。

如果你想使用较新的Apache的HttpClient的API与Android,你应该考虑使用的官方Android端口

Apache has deprecated DefaultHttpClient, but it seems this is not the case for Android, see also here 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!?

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.