Getting Started with iOS network programming: ASIHTTPRequest Asynchronous request

Source: Internet
Author: User

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]); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.