We run the program, if the network speed is very slow, the query will always black screen, until the end of the request screen to appear, so the user experience is very bad. Therefore, synchronous requests are generally used only in a child thread and not in the main threads. The user experience of an asynchronous request is better than a synchronization request, so there is generally a lot of asynchronous requests. The classic spin small icon for the network wait indicator appears on the status bar during the wait process, and the use of ASIHTTPRequest asynchronous requests achieves these effects without having to write additional code yourself.
ASIHTTPRequest and asiformdatarequest Two request classes can send asynchronous requests, Asiformdatarequest inherits the ASIHTTPRequest asynchronous request method, So we are focusing on asihttprequest asynchronous requests. The method by which the delegate object is invoked after the asynchronous request: Requestfinished: and requestfailed: implementation. The Startrequest method for modifying the main view controller MASTERVIEWCONTROLLER.M is as follows:
-(void) Startrequest {nsstring *strurl = [[NSString alloc] Initwithformat: @ "HTTP://IOSB ook3/mynotes/webservice.php?email=%@&type=%@&action=%@ ", @" < your iosbook1.com user's mailbox > ", @" JSON "@" Query "
];
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]];
ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url]; [Request Setdelegate:self]; ①[request startasynchronous]; ②}-(void) requestfinished: (ASIHTTPRequest *) request③{Nsdat
A *data = [request ResponseData]; Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]
;
[Self reloadview:resdict]; }-(void) requestfailed: (ASIHTTPRequest *) request④{nserror *error = [ RequEST ERROR];
NSLog (@ "%@", [Error localizeddescription]); }