ASIHTTPRequest Use understanding

Source: Internet
Author: User

Open Source Library: ASIHTTPRequest

ASIHTTPRequest Download URL: http://github.com/pokeb/asi-http-request

Dependent 5 library files: cfnetwork, systemconfiguration,mobilecoreservices, and Libz, LIBXML2

A class method is used to control the number of executions of a queue, but once a request is sent it is executed immediately and cannot be controlled logically.

[ASIHTTPRequest sharedqueue].maxconcurrentoperationcount = 1;

Initialization of the request queue

Self.queue = [Asinetworkqueue queue];

Number of concurrent executions

Self.queue.maxConcurrentOperationCount = 1;

let the queue go on, create a queue can have we control his logical order of execution

[Self.queue go];

sending an asynchronous request

-(void) Asynchronous: (nsurl *) URL

{

asihttprequest *request = [asihttprequest requestwithurl: url];

[Request Setrequestmethod:@ "GET"];

[Request Settimeoutseconds:];

// send asynchronous request Agent

//request.delegate = self;

Call block when request data is completed

[Request Setcompletionblock: ^{

nsdata *data = Request. ResponseData;

UIImage *image = [UIImage imagewithdata:d ATA];

self. Image = image;

if (request. Didusecachedresponse) {

NSLog(@ " to cache ");

}Else {

NSLog(@ " is coming to the network ");

}

}];

// request data failed call block

[Request Setfailedblock: ^{

NSLog(@ " request data failed ");

}];

//------------------------ set the cache policy -----------

nsstring *cachepath = [nshomedirectory()stringbyappendingpathcomponent:@ " Documents "];

asidownloadcache *cache = [[asidownloadcache alloc] init];

[Cache Setstoragepath: CachePath];

Cache. defaultcachepolicy = asionlyloadifnotcachedcachepolicy;

// This will erase the last storage at the time of each painting

Request. cachestoragepolicy = asicacheforsessiondurationcachestoragepolicy;

// Create a persistent cache here and call it directly from memory each time you want to call

//request.cachestoragepolicy = asicachepermanentlycachestoragepolicy;

//Request.downloadcache = cache;

// start asynchronous request

[Request startasynchronous];

// Add the request to the queue

//Handle to set the Get instance object

Appdelegate *appdelegate = [UIApplication sharedapplication].delegate;

[Appdelegate.queue Addoperation:request];

}

send a sync request

-(void) Synchronous: (nsurl *) URL

{

//ASIHTTPRequest *request = [ASIHTTPRequest requestwithurl:url];

asiformdatarequest *request = [asiformdatarequest requestwithurl: url];

[Request Setrequestmethod:@ "GET"];

[Request Settimeoutseconds:];

// Set the request header

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

// set cookier

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

// send a sync request

[Request startsynchronous];

// receive an error message

nserror *error = Request. Error;

if (Error = = Nil) {

nsdata *data = Request. ResponseData;

UIImage *image = [UIImage imagewithdata:d ATA];

self. Image = image;

}Else{

NSLog(@ " error sending sync request data :%@", error);

}

}

#pragma mark---asynchronous delegate

Completing an asynchronous request

-(void) requestfinished: (ASIHTTPRequest *) request

{

Get Data

NSData *data = Request.responsedata;

UIImage *image = [UIImage imagewithdata:data];

Self.image = image;

}

Asynchronous request failed

-(void) requestfailed: (ASIHTTPRequest *) request

{

Nserror *error = Request.error;

NSLog (@ "Asynchronous request failed:%@", error);

}

Other methods:

ASIHTTPRequest Use understanding

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.