ASIHTTPRequest Framework use Summary series of the A-hall Tutorial 3 (Asynchronous request)

Source: Internet
Author: User

in the previous section, Antioch and netizens shared the ASIHTTPRequest framework's synchronous request for Get,post. Obviously, if the network speed is slow, the query will always be very black screen, until the request to the end of the interface before the results, so the user experience must be very bad. As Antioch in the previous section, synchronization requests are typically used only in a child thread, not in the main thread.

ASIHTTPRequest and asiformdatarequest Two request classes can send asynchronous requests, which inherit the asynchronous request method of the former, so here Antioch Focus on asihttprequest asynchronous requests. The processing of an asynchronous request is requestfinished by means of a callback delegate object: or requestfailed: implemented. I. Asynchronous request for Get

-(void) startrequest

{

NSString *strurl = [[NSString alloc]initwithformat:@] http://www.crazyit.com/service/mynotes/webservice.php?email=% @&type=%@&action=%@ "@" [email protected], @ "JSON", @ "Query"];

Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring];

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

Set delegate object to self

[Request Setdelegate:self];

Self-defined callback method Requestsuccess, Requesterror (method name can be arbitrarily taken)

If the callback method is not set, as the next two lines do not need, the requestfinished is called by default, and the Requestfailed method

[Request Setdidfinishselector: @selector (requestsuccess:)];

[Request Setdidfailselector: @selector (requesterror:)];

[Request startasynchronous];

}

-(void) Requestsuccess: (ASIHTTPRequest *) request

{

NSData *data = [request ResponseData];

Nsdictionary *resdict = [nsjsonserialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:nil];

[Self reloadview:resdict];

}

-(void) Requesterror: (ASIHTTPRequest *) request

{

Nserror *error = [request ERROR];

NSLog (@ "%@", [Error localizeddescription]);

}

Reload Table View

-(void) Reloadview: (nsdictionary*) Res

{

NSNumber *resultcodeobj = [res objectforkey:@ "ResultCode"];

if ([Resultcodeobj IntegerValue] >=0)

{

Self.listdata = [Res objectforkey:@ "Record"];

[Self.tableview Reloaddata];

} else {

NSString *errorstr = [Resultcodeobj errormessage];

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "error message"

Message:errorstr

Delegate:nil

cancelbuttontitle:@ "OK"

Otherbuttontitles:nil];

[Alertview show];

}

}

Test

Two. Specifying a callback method for code block blocks for get asynchronous requests

-(void) startrequest

{

NSString *strurl = [[NSString alloc]initwithformat:@] http://www.crazyit.com/service/mynotes/webservice.php?email=% @&type=%@&action=%@ "@" [email protected], @ "JSON", @ "Query"];

Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring];

__weak asihttprequest *request = [ASIHTTPRequest Requestwithurl:url];

[Request setcompletionblock:^{

NSData *data = [request ResponseData];

Nsdictionary *resdict = [nsjsonserialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:nil];

[Self reloadview:resdict];

}];

[Request setfailedblock:^{

Nserror *error = [request ERROR];

NSLog (@ "%@", [Error localizeddescription]);

}];

[Request startasynchronous];

}

Reload Table View

-(void) Reloadview: (nsdictionary*) Res

{

if (Self.refreshcontrol) {

[Self.refreshcontrol endrefreshing];

Self.refreshControl.attributedTitle = [[nsattributedstring alloc]initwithstring:@ "dropdown refresh"];

}

NSNumber *resultcodeobj = [res objectforkey:@ "ResultCode"];

if ([Resultcodeobj IntegerValue] >=0)

{

Self.listdata = [Res objectforkey:@ "Record"];

[Self.tableview Reloaddata];

} else {

NSString *errorstr = [Resultcodeobj errormessage];

Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "error message"

Message:errorstr

Delegate:nil

cancelbuttontitle:@ "OK"

Otherbuttontitles:nil];

[Alertview show];

}

}

Description

setcompletionblock:^{...} and setfailedblcok:^{...} Called by the code block, where both methods are recalled on success and failure. There are a few minor problems with code blocks that can cause cyclic hold (retain cycle) problems. Cyclic saving is a memory problem, assuming that the A object holds the B object, and the B object retains the A object, neither a nor B can be released. In order to solve the code block looping problem. We used the _ _ weak key word before declaring the ASIHTTPRequest object, which means that the ASIHTTPRequest object is a weak reference, which is similar to what is said in Java, and does not hold processing. This method is suitable for the ARC memory management mode after ios5. In the case of MRC memory management, you need to use the _block keyword before the ASIHTTPRequest object.

Three. For the async mode of post, like get, I don't say much. You can refer to the method of synchronization in the previous article, a little change can be.

ASIHTTPRequest Framework use Summary series of the A-hall Tutorial 3 (Asynchronous request)

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.