This is a lightweight asynchronous HTTP client powered by OkHttp but with a significantly simplified and easier to use API design.
The goal of this library is to have an API that clearly and cleanly supports the following features:
- Asynchronous network requests without any need for manual thread handling
- Response
onSuccess
callbacks run on the mainthread (by default) - Easy way to catch all errors and failures and handle them
- Easy pluggable Text, JSON, and Bitmap response handlers to parse the response
This client tries to follow a similar API inspired by this older now deprecated android-async-http library.
To use this library, add the following to your .gradle
file:
dependencies {
implementation 'com.codepath.libraries:asynchttpclient:2.1.1'
}
-
Make sure all network calls are using
https://
instead ofhttp://
-
Verify that network access is allowed via the
<uses-permission>
:<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codepath.example"> <uses-permission android:name="android.permission.INTERNET" />```
-
Add any networking calls by leveraging the
AsyncHttpClient
AsyncHttpClient client = new AsyncHttpClient(); client.get("https://api.thecatapi.com/v1/images/search", new TextHttpResponseHandler() { @Override public void onSuccess(int statusCode, Headers headers, String response) { Log.d("DEBUG", response); } @Override public void onFailure(int statusCode, @Nullable Headers headers, String errorResponse, @Nullable Throwable throwable) { Log.d("DEBUG", errorResponse); } });
This example uses
TextHttpResponseHandler
which presents the response as raw text. We could use theJsonHttpResponseHandler
instead to have the API response automatically parsed for us into JSON. See other example calls here.
For more detailed usages, check out the CodePath Async Http Client Guide.