The open source Asynchttp is a library based on the Apache HTTP client wrapper, which is relatively simple to retrieve data without having to handle threading and instantiation handler
I wrote a asynchttpclient. Get pictures and web data demonstrate simple usage
Asynchttpclient.java is the exposed interface, the most important is the post and get 2 kinds, there are other HTTP request methods such as Delete,patch operation, not very common
The thread pool is executors.newcachedthreadpool () This source shows the core thread is 0, the thread created after 60 seconds do not have to destroy, feel more in line with the network frequent operation for a period of time,
such as the listview asynchronous download bitmap pictures
public static Executorservice Newcachedthreadpool () {
return new Threadpoolexecutor (0, Integer.max_value,
60L, Timeunit.seconds,
New synchronousqueue<runnable> ());
}
RequestHandle The requested run object, you can cancel a request, but not necessarily succeed.
Create an asynchronous request for task execution
Protected Asynchttprequest newasynchttprequest (Defaulthttpclient client, HttpContext HttpContext, Httpurirequest Urirequest, String ContentType, Responsehandlerinterface ResponseHandler, Context context) {return new Asynchttpreque St (client, HttpContext, Urirequest, ResponseHandler);} Send request protected RequestHandle SendRequest (Defaulthttpclient client, HttpContext HttpContext, Httpurirequest UriReques T, String ContentType, Responsehandlerinterface ResponseHandler, Context context) {//Judgment code omitted ..... Just look at the key if (contentType! = null) {if (urirequest instanceof httpentityenclosingrequestbase && (H Ttpentityenclosingrequestbase) urirequest). GetEntity ()! = null) {LOG.W (Log_tag, "Passed ContentType would b E ignored because httpentity sets content type "); } else {Urirequest.setheader (header_content_type, ContentType); }} responsehandler.setrequestheaders (Urirequest.getallheaders ()); ReSponsehandler.setrequesturi (Urirequest.geturi ()); Constructs a Request object, the thread runs asynchttprequest request = newasynchttprequest (client, HttpContext, Urirequest, ContentType, respon Sehandler, context); Threadpool.submit (request);//Submit task RequestHandle RequestHandle = new RequestHandle (request); This place is used to determine if there is a request before retrieving an unnecessary request object, because RequestHandle can cancel the task, using the weak object if (context! = NULL) {//ADD request T O Request Map List<requesthandle> requestlist = requestmap.get (context); Synchronized (Requestmap) {if (requestlist = = null) {requestlist = Collections.synchro Nizedlist (New linkedlist<requesthandle> ()); Requestmap.put (context, requestlist); }} requestlist.add (RequestHandle); iterator<requesthandle> Iterator = Requestlist.iterator (); while (Iterator.hasnext ()) {if (Iterator.next (). shouldbegarbagecollected ()) {//cleanup of unused weak objects iterator.remove (); }}} return requesthandle; }
Constructs a request to return an object, you can cancel the request
RequestHandle RequestHandle = new RequestHandle (request);
The Responsehandlerinterface callback interface defines the start of the task, the process, the end, and the error message
Where is the UI message sent? Inside the abstract class Asynchttpresponsehandler constructor
The handler instantiation inside the UI thread is inside the constructor, the default is Mylooper, and the default constructor is the null public Asynchttpresponsehandler (Looper Looper) {This.loo per = Looper = = null? Looper.mylooper (): Looper; Use asynchronous mode by default. The default asynchronous Setusesynchronousmode (FALSE); Do not use the pool's thread to fire callbacks by default. Setusepoolthread (FALSE); } @Override public void Setusesynchronousmode (Boolean sync) {//A Looper must is prepared before setting Asynchronous mode. if (!sync && looper = = null) {sync = true; LOG.W (Log_tag, "current thread had not called Looper.prepare (). Forcing synchronous mode. "); If using asynchronous mode. if (!sync && handler = = null) {//Create a handler on current thread to submit tasks handle R = New Responderhandler (this, looper);//Initialize, see Responderhandler} else if (sync && handler! = null) { Todo:considerAdding a flag to remove all queued messages. handler = NULL; } Usesynchronousmode = sync; } private static Class Responderhandler extends Handler {private final Asynchttpresponsehandler MR Esponder; Responderhandler (Asynchttpresponsehandler mresponder, Looper Looper) {super (Looper); This.mresponder = Mresponder; } @Override public void Handlemessage (message msg) {mresponder.handlemessage (msg);//Where the final commit message is processed } }
There are multiple subclasses of Asynchttpresponsehandler, including Binary,text,json callbacks
UML Simple class Diagram
Asynchttpclient Source Code Analysis