IOS network programming-ASIHTTPRequest asynchronous request

Source: Internet
Author: User

We run the program. If the network speed is slow, black screen will appear during the query until the request ends, so that the user experience is poor. Therefore, synchronous requests are generally used only in a child thread, rather than in the main thread. The user experience of asynchronous requests is better than that of synchronous requests. Therefore, many asynchronous requests are usually used. During the waiting process, the classic spin-down icon of the network wait indicator appears on the status bar, And the ASIHTTPRequest asynchronous request is used to achieve these effects without writing additional code.

 
 


ASIHTTPRequest and ASIFormDataRequest can both send asynchronous requests. ASIFormDataRequest inherits ASIHTTPRequest asynchronous request methods, so we will focus on ASIHTTPRequest asynchronous requests. After an asynchronous request is sent, the method of the delegate object is called back: requestFinished: And requestFailed. The startRequest Method for modifying master View Controller MasterViewController. m is as follows:

[Cpp]
-(Void) startRequest
 
{
 
NSString * strURL = [[NSString alloc]
 
InitWithFormat:
 
@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",
 
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
 
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
 
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
 
[Request setDelegate: self]; ①
 
[Request startAsynchronous]; ②
 
}
 
-(Void) requestFinished :( ASIHTTPRequest *) request ③
 
{
 
NSData * 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]);
 
}

-(Void) startRequest

{

NSString * strURL = [[NSString alloc]

InitWithFormat:

@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",

@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];

NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];

ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];

[Request setDelegate: self]; ①

[Request startAsynchronous]; ②

}

-(Void) requestFinished :( ASIHTTPRequest *) request ③

{

NSData * 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]);

}


The Code in line ① [request setDelegate: self] sets the delegate object to self, and then initiates an asynchronous request in line ② [request startAsynchronous, if the server returns a successful result, it calls back the requestFinished of row ③: method. If a failure occurs, it calls back the requestFailed of line ④: method. Their parameters are of the ASIHTTPRequest type. These two methods are the default callback method. You can also customize the callback method. Therefore, the above Code can also be changed to the following form:

[Cpp]
-(Void) startRequest
 
{
 
... ...
 
[Request setDidFinishSelector: @ selector (requestSuccess :)];
 
[Request setDidFailSelector: @ selector (requestError :)];
 
[Request startAsynchronous];
 
}
 
-(Void) requestSuccess :( ASIHTTPRequest *) request
 
{
 
NSData * data = [request responseData];
 
NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data
 
Options: NSJSONReadingAllowFragments error: nil];
 
[Self reloadView: resDict];
 
}
 
-(Void) requestError :( ASIHTTPRequest *) request
 
{
 
NSError * error = [request error];
 
NSLog (@ "% @", [error localizedDescription]);
 
}

-(Void) startRequest

{

... ...

[Request setDidFinishSelector: @ selector (requestSuccess :)];

[Request setDidFailSelector: @ selector (requestError :)];

[Request startAsynchronous];

}

-(Void) requestSuccess :( ASIHTTPRequest *) request

{

NSData * data = [request responseData];

NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data

Options: NSJSONReadingAllowFragments error: nil];

[Self reloadView: resDict];

}

-(Void) requestError :( ASIHTTPRequest *) request

{

NSError * error = [request error];

NSLog (@ "% @", [error localizedDescription]);

}


SetDidFinishSelector of the request object: Specifies the method for successful callback. setDidFailSelector: Specifies the failure method to be called back.

In asynchronous requests, you can use Block to specify the callback method in the Block to make the code more clean. The startRequest method of MasterViewController. m, which uses the Code's main View Controller, is as follows:

[Cpp]
-(Void) startRequest
 
{
 
NSString * strURL = [[NSString alloc] initWithFormat:
 
@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =%@ ", @" <your iosbook1.com user email address> ", @" JSON ", @" query "];
 
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
 
_ Weak ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url]; ①
 
[Request setCompletionBlock: ^ {②
 
NSData * data = [request responseData]; ③
 
NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data
 
Options: NSJSONReadingAllowFragments error: nil];
 
[Self reloadView: resDict];
 
}];
 

 
[Request setFailedBlock: ^ {④
 
NSError * error = [request error];
 
NSLog (@ "% @", [error localizedDescription]);
 
}];
 
[Request startAsynchronous];
 
}

-(Void) startRequest

{

NSString * strURL = [[NSString alloc] initWithFormat:

@ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =%@ ", @" <your iosbook1.com user email address> ", @" JSON ", @" query "];

NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];

_ Weak ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url]; ①

[Request setCompletionBlock: ^ {②

NSData * data = [request responseData]; ③

NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data

Options: NSJSONReadingAllowFragments error: nil];

[Self reloadView: resDict];

}];

 

[Request setFailedBlock: ^ {④

NSError * error = [request error];

NSLog (@ "% @", [error localizedDescription]);

}];

[Request startAsynchronous];

}

 

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.