Objective
About Android-async-http Many, I believe we all have a certain understanding. I am here to simply say it's cancellation request this piece. All of the conclusions are in the same context.
Requests that can be canceled
Android-async-http requests that can be canceled must pass in a context, otherwise there is no effect. Take the simple GET request as an example.
Get (context, URL, params, responsehandler);
Here is a network request class:
/** * Network request * @author Yang * @date 2015 March 16 PM 2:58:13 */ public class fmhttpclient { private asynchttpclient client; Public fmhttpclient () { client = new asynchttpclient (); } /** * gets the Asynchttpclient object. * @return */ public asynchttpclient getasynchttpclient () { return client; } /** * get request. * @param context * @param url * @param params * @param responseHandler * @return requesthandle */ public requesthandle get (Context context,string url, requestparamparams, Responsehandlerinterface responsehandler) { return client.get (context, Url, params, responseHandler); } /** * cancel the network request. * @param context * @param mayInterruptIfRunning */ Public void cancelrequests (context context,boolean mayinterruptifrunning) { Client.cancelrequests (context, mayinterruptifrunning); } /** * Cancels all requests. * @param mayInterruptIfRunning */ public void Cancelallrequests (boolean mayinterruptifrunning) { client.cancelallrequests ( mayinterruptifrunning); } }
3. Three ways to cancel a request
(1) The first kind of cancellation request
CancelRequests (Context Context, Boolean mayinterruptifrunning);//In Asynchttpclient
(2) The second kind of cancellation request
Cancelallrequests (Boolean mayinterruptifrunning);//In Asynchttpclient
(3) The third kind of cancellation request
Cancel (Boolean mayinterruptifrunning)//In RequestHandle
See the main program implementation class below:
public class mainactivity extends activity { @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (r.layout.activity_main); string url1 = "https ://www.baidu.com/"; fmhttpclient client = new fmhttpclient (); Requesthandle rd1 = client.get (this, url1, null, new Asynchttpresponsehandler () { @Override public void onsuccess (int  ARG0, HEADER[] ARG1, BYTE[] ARG2) { system.out.println (" content1 size: "+arg2.length); } @Override public void onfailure (INT ARG0, HEADER[] ARG1, BYTE[] ARG2,      THROWABLE ARG3) { system.out.println ("OnFailure1"); } @Override public void onfinish () { System.out.println ("OnFinish1"); } }); string url2 = "http://www.hao123.com/"; requesthandle rd2 = Client.get (This, url2, null, new asynchttpresponsehandler () { @Override public void onsuccess (int arg0, header[] arg1, byte[] ARG2) { system.out.println ("content2 size: " +arg2.length); } @Override public void onfailure (int  ARG0, HEADER[] ARG1, BYTE[] ARG2,     THROWABLE ARG3) { system.out.prinTLN ("OnFailure2"); } @Override public void onfinish () { system.out.println ("OnFinish2"); } }); //rd1.cancel (true); //client.cancelrequests (This, true); //client.cancelallrequests (true); system.out.println ("result1-->" + Rd1.iscancelled ()); system.out.println ("Result2-->" +rd2.iscancelled ()); }}
Two or more than two requests:
With the first and second, the print result is true, and two requests are canceled. If the requirement requires that each cancellation request does not affect each other, then the two-clock approach is undesirable, and the requirement is that all requests are canceled, both of which can be used, but the second option is preferred.
With a third cancellation request that corresponds to one of the requests, the request is canceled and the other requests are not affected.
A request:
Three different ways to use it casually.
Android-async-http Cancel Request