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!?
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.
You need to add this line in your app Gradle
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With