HttpClient is deprecated (android studio, Target=api 22)
Stop using it and use URLConnection instead. It's been 4 years Google recommends this.
http://android-developers.blogspot.be/2011/09/androids-http-clients.html
If you want an external library with a nicer API, you can try OkHttp: http://square.github.io/okhttp/
No more support for Apache HttpClient in marshmallow
Use HttpURLConnection instead.
Or some libraries like:
Retrofit: http://square.github.io/retrofit/
Volley: https://developer.android.com/training/volley/index.html
OkHttp: http://square.github.io/okhttp/
Read this article about comparision: Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
Deprecated Java HttpClient - How hard can it be?
Relevant imports:
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;
Usage:
HttpClient httpClient = HttpClientBuilder.create().build();
EDIT (after Jules' suggestion):
As the build()
method returns a CloseableHttpClient
which is-a AutoClosable
, you can place the declaration in a try-with-resources statement (Java 7+):
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
// use httpClient (no need to close it explicitly)
} catch (IOException e) {
// handle
}
Related Topics
Failed to Load Appcompat Actionbar with Unknown Error in Android Studio
Android Writing Logs to Text File
Android API 21 Toolbar Padding
Android - Camera Preview Is Sideways
How to Add Stacktrace or Debug Option When Building Android Studio Project
Android: Scale a Drawable or Background Image
How to Create a Custom Navigation Drawer in Android
How to Lock/Unlock Screen Programmatically
Android Studio Marks R in Red with Error Message "Cannot Resolve Symbol R", But Build Succeeds
Accessing External Storage in Android API 29
Android Studio's Project Gradle File Changed
Firebase Firestore Variable Name Changed
Android Material Design Button Styles
Android Edittext Delete(Backspace) Key Event