ASIHTTPRequest Framework use Summary series of the A-Hall tutorial 2 (Sync request)

Source: Internet
Author: User

Starting with this article, Antioch is combined with some specific code to illustrate. In the ASIHTTPRequest framework, the classes associated with HTTP requests have asihttprequest and asiformdatarequest, the most common of which is asihttprequest, asiformdatarequest is a subclass of ASIHTTPRequest, you can send HTML-like form data, you can upload data, by default, other httpy request way. They can both make asynchronous or synchronous requests.

In terms of performance and user experience, Antioch is not recommended for synchronization, and it is recommended to use asynchronous methods. That is, in the main thread is generally used asynchronously, in the child thread to use synchronous mode. However, from the point of view of technical learning, here Antioch still want to introduce the way get and post synchronization.I. Implementing a GET Synchronization requestThe code is generally written as follows

-(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];

[Request startsynchronous]; Sync Request

NSLog (@ "request complete ...");

Nserror *error = [request ERROR];

If the Error object is not empty, there is an error in the request process

if (!error) {

NSString *response = [request responsestring];

NSData *data = [request ResponseData];

The return value is NSData, and this type is suitable for nsjsonserialization parsing

//If you want to return a string, you can use NSString *data = [request responsestring];

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

[Self reloadview:resdict];

}

}

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];

}

}

When using the ASIHTTPRequest class, you need to introduce a header file ASIHTTPRequest.hTwo. Implementing a post Sync request

-(void) startrequest

{

NSString *strurl = @ "http://www.crazyit.com/service/mynotes/webservice.php";

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

Asiformdatarequest *request = [Asiformdatarequest Requestwithurl:url];

[Request setpostvalue:@ "[email protected]" forkey:@ "email"];

[Request setpostvalue:@ "JSON" forkey:@ "type"];

[Request setpostvalue:@ "query" forkey:@ "action"];

[Request startsynchronous];

NSLog (@ "request complete ...");

Nserror *error = [request ERROR];

if (!error) {

NSString *response = [request responsestring];

NSData *data = [request ResponseData];

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

[Self reloadview:resdict];

}

}

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];

}

}

ASIHTTPRequest Framework use Summary series of the A-Hall tutorial 2 (Sync 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.