Sync requests and asynchronous requests for ASIHTTPRequest in iOS

Source: Internet
Author: User


1. First join the ASI Open Source Library


2.

WebImageView.h

#import <UIKit/UIKit.h>

#import "ASIHTTPRequest.h"

@interface Webimageview: Uiimageview <asihttprequestdelegate>


-(void) Setimageurl: (nsurl *) URL;


@end


webimageview.m


#import "WebImageView.h"

#import"ASIHTTPRequest.h"


@implementation Webimageview


-(ID) initWithFrame: (cgrect) frame

{

self = [superinitwithframe: frame];

if (self) {

//initialization code

}

returnself;

}


-(void) Setimageurl: (nsurl *) url{

// sync request

//[self Synchronous:url];

// asynchronous Request

[SelfAsynchronous: url];

}


// sync request

-(void) Synchronous: (nsurl *) url{

asihttprequest *request = [asihttprequestrequestwithurl : url];

[Request Setrequestmethod:@ "GET"];

[Request settimeoutseconds:];

// Set the request header

//[request setrequestheaders:<# (nsmutabledictionary *) #>];

// set cookies

//[request setrequestcookies:<# (Nsmutablearray *) #>];

// send a sync request

[Request startsynchronous];

nserror *error = [requesterror];

if (Error = =Nil) {

// get the requested data

nsdata *data = Request. responsedata;

UIImage *image = [UIImageimagewithdata:d ATA];

Self. image = image;

}Else{

NSLog (@ " Network request failed %@", error);

}


}


// Asynchronous request

-(void) Asynchronous: (nsurl *) url{

asihttprequest *request = [asihttprequestrequestwithurl: url];

[Request Setrequestmethod:@ "GET"];

[Request settimeoutseconds:];

Request. delegate =self;

[Request startasynchronous];

}



#pragma mark ASIHTTPRequest Delegate implementation of the Protocol method------asynchronous request ---------


// Request data completion

-(void) requestfinished: (asihttprequest *) request{

nsdata *data = Request. ResponseData;

Self. image = [UIImageimagewithdata:d ATA];

}


// request failed

-(void) requestfailed: (asihttprequest *) request{

nserror *error = [requesterror];

NSLog (@ " Network request failed %@", error);

}


@end


3, the use of block mode of network asynchronous request, at this time do not need to implement delegate

// Asynchronous request

-(void) Asynchronous: (nsurl *) url{

asihttprequest *request = [asihttprequestrequestwithurl: url];

[Request Setrequestmethod:@ "GET"];

[Request settimeoutseconds:];

// set block after network request is complete do not need delegate at this time

[Request Setcompletionblock: ^{

nsdata *data = Request. ResponseData;

Self. image = [UIImageimagewithdata:d ATA];

}];

// set block for request failed call

[RequestSetfailedblock: ^{

nserror *error = [requesterror];

NSLog (@ " Network request failed %@", error);


}];

[Request startasynchronous];

}




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.