- With sync version of apache http client, the through put is about 200 requests/sec. The average response time is about 100ms/request. I abort the requests after a max of 180ms. After switching to Async, the response time went up by 20ms/request.
- Akka Http; Apache HttpClient 4; Apache Async HttpClient 4; Apache HttpClient 5; Apache Async HttpClient 5; Apache CXF Jax RS; Apache CXF with Conduit; Async Http Client; Dispatch Reboot; ElasticSearch; Feign; Fuel; gRPC; Google HttpClient; Http4k with Apache HttpClient 4; Http4k with Apache Async HttpClient 4; Http4k with Apache HttpClient 5.
Overview
Update to Apache Async HTTP client 4.1 when released. Update to version 4.1 of the Apache client. The test should pass again.
An asynchronous callback-based Http client for Android built on top of Apache’sHttpClient libraries.All requests are made outside of your app’s main UI thread, but any callbacklogic will be executed on the same thread as the callback was created usingAndroid’s Handler message passing. You can also use it in Service or background thread,library will automatically recognize in which context is ran.
If you are also looking for a great android crash reporting service, I’d also recommend checking out my company, Bugsnag.
Features
- Using upstream HttpClient of version 4.3.6 instead of Android provided DefaultHttpClient
- Compatible with Android API 23 and higher
- Make asynchronous HTTP requests, handle responses in anonymous callbacks
- HTTP requests happen outside the UI thread
- Requests use a threadpool to cap concurrent resource usage
- GET/POST params builder (RequestParams)
- Multipart file uploads with no additional third party libraries
- Streamed JSON uploads with no additional libraries
- Handling circular and relative redirects
- Tiny size overhead to your application, only 90kb for everything
- Automatic smart request retries optimized for spotty mobile connections
- Automatic gzip response decoding support for super-fast requests
- Binary protocol communication with
BinaryHttpResponseHandler
- Built-in response parsing into JSON with
JsonHttpResponseHandler
- Saving response directly into file with
FileAsyncHttpResponseHandler
- Persistent cookie store, saves cookies into your app’s SharedPreferences
- Integration with Jackson JSON, Gson or other JSON (de)serializing libraries with
BaseJsonHttpResponseHandler
- Support for SAX parser with
SaxAsyncHttpResponseHandler
- Support for languages and content encodings, not just UTF-8
Used in Production By Top Apps and Developers
- Instagram is the #1 photo app on android, with over 10million users
- Popular online pinboard. Organize and share things you love.
- Frontline Commando (Glu Games)
- #1 first person shooting game on Android, by Glu Games.
- Heyzap
- Social game discovery app with millions of users
- Pose
- Pose is the #1 fashion app for sharing and discovering new styles
- Thousands more apps…
- Async HTTP is used in production by thousands of top apps.
Installation & Basic Usage
Add maven dependency using Gradle buildscript in format
Import the http package.
![Apache Apache](https://i.stack.imgur.com/0IsR2.png)
Create a new AsyncHttpClient
instance and make a request:
Recommended Usage: Make a Static Http Client
In this example, we’ll make a http client class with static accessors to makeit easy to communicate with Twitter’s API.
![Async Async](https://miro.medium.com/max/2566/1*vPgcWpdshV6ut3Nz3izmzg.png)
This then makes it very easy to work with the Twitter API in your code:
Check out theAsyncHttpClient,RequestParams andAsyncHttpResponseHandlerJavadocs for more details.
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore
which is an implementationof the Apache HttpClient CookieStore
interface that automatically savescookies to SharedPreferences
storage on the Android device.
This is extremely useful if you want to use cookies to manage authenticationsessions, since the user will remain logged in even after closing andre-opening your app.
First, create an instance of AsyncHttpClient
:
Now set this client’s cookie store to be a new instance ofPersistentCookieStore
, constructed with an activity or application context(usually this
will suffice):
Any cookies received from servers will now be stored in the persistent cookiestore.
To add your own cookies to the store, simply construct a new cookie andcall addCookie
:
See the PersistentCookieStore Javadocfor more information.
![Apache Async Http Client Apache Async Http Client](https://miro.medium.com/max/964/1*M6RMsATK5VVrXeeyQBNZnA.png)
Adding GET/POST Parameters with RequestParams
The RequestParams
class is used to add optional GET or POST parameters toyour requests. RequestParams
can be built and constructed in various ways:
Create empty RequestParams
and immediately add some parameters:
Create RequestParams
for a single parameter:
Create RequestParams
from an existing Map
of key/value strings:
See the RequestParams Javadocfor more information.
![Portal Portal](https://alfach.com/wp-content/uploads/2018/05/android-developers-working.jpg)
Uploading Files with RequestParams
The RequestParams
class additionally supports multipart file uploads asfollows:
Add an InputStream
to the RequestParams
to upload:
Add a File
object to the RequestParams
to upload:
Apache Async Http Client
Add a byte array to the RequestParams
to upload:
See the RequestParams Javadocfor more information.
Downloading Binary Data with FileAsyncHttpResponseHandler
The FileAsyncHttpResponseHandler
class can be used to fetch binary data suchas images and other files. For example:
See the FileAsyncHttpResponseHandler Javadocfor more information.
Adding HTTP Basic Auth credentials
Some requests may need username/password credentials when dealing with API services that use HTTP Basic Access Authentication requests. You can use the method setBasicAuth()
to provide your credentials.
Set username/password for any host and realm for a particular request.By default the Authentication Scope is for any host, port and realm.
You can also provide a more specific Authentication Scope (recommended)
See the RequestParams Javadocfor more information.
Testing on device
You can test the library on real device or emulator using provided Sample Application.Sample application implements all important functions of library, you can use it as source of inspiration.
Source code of sample application: https://github.com/android-async-http/android-async-http/tree/master/sample
To run sample application, clone the android-async-http github repository and run command in it’s root:
Which will install Sample application on connected device, all examples do work immediately, if notplease file bug report on https://github.com/android-async-http/android-async-http/issues
Building from Source
To build a .jar
file from source, first make a clone of the android-async-http github repository. Then you have to have installedAndroid SDK and Gradle buildscript, then just run:
This will generate a file in path {repository_root}/library/build/libs/library-1.4.9.jar
.
Reporting Bugs or Feature Requests
Please report any bugs or feature requests on the github issues page for thisproject here:
Credits & Contributors
- James Smith (https://github.com/loopj)
- Creator and Maintainer
- Marek Sebera (https://github.com/smarek)
- Maintainer since 1.4.4 release
- Noor Dawod (https://github.com/fineswap)
- Maintainer since 1.4.5 release
- Luciano Vitti (https://github.com/xAnubiSx)
- Collaborated on Sample Application
- Jason Choy (https://github.com/jjwchoy)
- Added support for RequestHandle feature
- Micah Fivecoate (https://github.com/m5)
- Major Contributor, including the original
RequestParams
- The Droid Fu Project (https://github.com/kaeppler/droid-fu)
- Inspiration and code for better http retries
- Rafael Sanches (https://blog.rafaelsanches.com)
- Original
SimpleMultipartEntity
code - Anthony Persaud (https://github.com/apersaud)
- Added support for HTTP Basic Authentication requests.
- Linden Darling (https://github.com/coreform)
- Added support for binary/image responses
![Apache Async Http Client Apache Async Http Client](https://rieckpil.de/wp-content/uploads/2020/12/test-java-http-client-usages.png)
And many others, contributions are listed in each file in license header.You can also find contributors by looking on project commits, issues and pull-requests on Github
License
Apache Async Http Client Portal
The Android Asynchronous Http Client is released under the Android-friendlyApache License, Version 2.0. Read the full license here:
About the Author
James Smith, British entrepreneur and developer based in San Francisco.
Apache Async Http Client Completablefuture
I'm the co-founder of Bugsnag with Simon Maynard, and from 2009 to 2012 I led up the product team as CTO of Heyzap.
![](https://cdn-ak.f.st-hatena.com/images/fotolife/r/ruriatunifoefec/20200910/20200910011346.png)