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)