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
onSuccesscallbacks 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>:<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.codepath.example"> <uses-permissionandroid:name="android.permission.INTERNET" />```
Add any networking calls by leveraging the
AsyncHttpClientAsyncHttpClientclient = newAsyncHttpClient(); client.get("https://api.thecatapi.com/v1/images/search", newTextHttpResponseHandler() { @OverridepublicvoidonSuccess(intstatusCode, Headersheaders, Stringresponse) { Log.d("DEBUG", response); } @OverridepublicvoidonFailure(intstatusCode, @NullableHeadersheaders, StringerrorResponse, @NullableThrowablethrowable) { Log.d("DEBUG", errorResponse); } });
This example uses
TextHttpResponseHandlerwhich presents the response as raw text. We could use theJsonHttpResponseHandlerinstead 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.