AsyncHttpClient by Callum Taylor
This is the new and improved version of
AsyncHttpClient taken from X-Library. It was long due a re-write.
In this version it allows a more flexible usage of posting files, http entities and GZIP handling.
The library uses Square's OKHTTP library.
It consists of 2 different classes,
SyncHttpClient. Obviously by the name,
AsyncHttpClient is for asynchronous requests which uses the
AsyncTask paradigm, and
SyncHttpClient is for synchronous requests which should be handled by yourself in a thread outside of the UI thread.
Simply import the gradle file into Android Studio and reference the dependancy in your
settings.gradle and project's
Alternatively you can add the maven dependancy
net.callumtaylor:asynchttpclient:2.1.1 N.B. Do NOT use
net.callumtaylor.asynchttp for the group ID. This is an old ID and will NOT be updated
Table of contents
Downloading large files
In order to download large files, you will need to subclass
AsyncHttpResponseHandler and override the
onByteChunkReceived() method to write directly to cache instead of appending to a
ByteArrayOutputStream which is what the standard
BinaryResponseHandler does. This is to stop OOM due to a over-sized output stream.
AsyncTask, only one instance can be created at a time. If one client makes 2 requests, the first request is canceled for the new request. You can either wait for the first to finish before making the second, or you can create two seperate instances. See: Example custom handler for more.
SyncHttpClient is a paramitized class which means the type you infer to it, is the type that gets returned when calling the method. When supplying a ResponseHandler, that ResponseHandler must also paramitized with the same type as the
You can also get the info of the request by calling your
SyncHttpClient instance and
getConnectionInfo(). This can only be called after the response has been completed.
Note: Because Android requires all network requests to be performed outside the UI thread, you must use SyncHttpClient sparingly and make sure you handle the operaion OFF the UI thread.
Because of the nature of REST,
DELETE requests behave in the same
PUT requests also behave in the same way.