IOS network programming-asihttprequest example-Data Request queue

Source: Internet
Author: User

Instance: Request queue

We use an example to introduce the use of the Request queue. We have designed an application. the user clicks the go button to download two images from the server at the same time.

 

Let's take a look at the main View Controller viewcontroller. h Code as follows:

#import “ASIHTTPRequest.h”#import “ASINetworkQueue.h”#import “NSNumber+Message.h”#import “NSString+URLEncoding.h” @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *imageView1;@property (weak, nonatomic) IBOutlet UIImageView *imageView2;@property (strong) ASINetworkQueue  *networkQueue; - (IBAction)onClick:(id)sender;@end

 

 

We need to introduce the asihttprequest. h and asinetworkqueue. h header files of the ASI framework. Imageview1 and imageview2 are two image View Controls corresponding to the screen. The networkqueue attribute of the asinetworkqueue type is also defined. Let's take a look at the calling method by clicking the go button in the viewcontroller. m of the main view controller. The Code is as follows:

-(Ibaction) onclick :( ID) sender {If (! _ Networkqueue) {_ networkqueue = [[asinetworkqueue alloc] init]; ①} // stop a previous queue [_ networkqueue cancelalloperations]; ② // create an ASI queue [_ networkqueue setdelegate: self]; [_ networkqueue setrequestdidfinishselector: @ selector (requestfinished :)]; ③ [_ networkqueue setrequestdidfailselector: @ selector (requestfailed :)]; ④ [_ networkqueue failed: @ selector (queuefinished :)]; ⑤ For (INT I = 1; I <3; I ++) {nsst Ring * strurl = [[nsstring alloc] initwithformat: @ "http: // iosbook3/download. php? Response ", @" <your iosbook1.com user email> ", I]; nsurl * url = [nsurl urlwithstring: [strurl urlencodedstring]; asihttprequest * request = [asihttprequest requestwithurl: url]; request. tag = I; ⑥ [_ networkqueue addoperation: request]; 7} [_ networkqueue go]; queue}

 

Let's look at their callback methods and code:

-(Void) requestfinished :( asihttprequest *) Request {nsdata * Data = [Request responsedata]; nserror * eror; nsdictionary * resdict = [nsjsonserialization jsonobjectwithdata: dataoptions: response error: & eror]; If (! Resdict) {uiimage * IMG = [uiimage imagewithdata: Data]; If (request. tag = 1) {① _ imageview1.image = IMG;} else {_ imageview2.image = IMG;} else {nsnumber * resultcodeobj = [resdict objectforkey: @ "resultcode"]; nsstring * errorstr = [resultcodeobj errormessage]; uialertview * alertview = [[uialertview alloc] initwithtitle: @ "error message" message: errorstrdelegate: @ "OK" response: Nil]; [alertview show];} If ([_ networkqueue requestscount] = 0) {② [self setnetworkqueue: Nil];} nslog (@ "request succeeded ");} -(void) requestfailed :( asihttprequest *) Request ③ {nserror * error = [Request Error]; nslog (@ "% @", [error localizeddescription]); if ([_ networkqueue requestscount] = 0) {[self setnetworkqueue: Nil];} nslog (@ "request failed");}-(void) queuefinished :( asihttprequest *) request ④ {If ([_ networkqueue requestscount] = 0) {[self setnetworkqueue: Nil];} nslog (@ "queue completed ");}

 

 

Requestfinished: The method is the successful callback method of the request object. Therefore, two request objects are called twice, in the code line ①, we click the tag attribute of the request object set by the go button to determine which request object is called back. Load the image to display different image views. Code ② [_ networkqueue requestscount] can determine the number of request objects in the queue.

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.