Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClientBuilder missing on Android?

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!?

like image 790
Oliver Hausler Avatar asked Jan 09 '23 04:01

Oliver Hausler


2 Answers

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.

like image 181
ok2c Avatar answered Jan 19 '23 06:01

ok2c


You need to add this line in your app Gradle

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
like image 23
Saleem Avatar answered Jan 19 '23 06:01

Saleem