Prior knowledge-what is ASIHTTPRequest?
Using the HTTP Network request API in the iOS SDK is quite complicated and cumbersome to call, ASIHTTPRequest is a set of APIs that encapsulates the Cfnetwork API and is very simple to use, written in Objective-c, can be very well applied in Mac OS x systems and iOS platform applications. ASIHTTPRequest applies to basic HTTP requests, and to the interaction between rest-based services.
How do I use ASIHTTPRequest?
There are many articles on the internet dedicated to the use of asihttprequest, very detailed, the landlord will not repeat the HA, here gives a classic introduction of the detailed article link: http://www.cnblogs.com/dotey/archive/2011/05/10/2041966.html
Uploading JSON-formatted data
First, the main function code snippet is given, and then the code is parsed in detail:
[CPP]View Plaincopy
- Nsdictionary *user = [[Nsdictionary alloc] initwithobjectsandkeys:@"0" @"Version", nil];
- if ([nsjsonserialization Isvalidjsonobject:user])
- {
- Nserror *error;
- NSData *jsondata = [nsjsonserialization datawithjsonobject:user options:nsjsonwritingprettyprinted error: &error] ;
- Nsmutabledata *tempjsondata = [Nsmutabledata datawithdata:jsondata];
- //nslog (@ "Register json:%@", [[NSString alloc] Initwithdata:tempjsondata encoding:nsutf8stringencoding]);
- Nsurl *url = [Nsurl urlwithstring:@"http://42.96.140.61/lev_version.php"];
- ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
- [Request addrequestheader:@"Content-type" value:@ "Application/json; Encoding=utf-8 "];
- [Request addrequestheader:@"Accept" value:@"Application/json"];
- [Request setrequestmethod:@"POST"];
- [Request Setpostbody:tempjsondata];
- [Request startsynchronous];
- Nserror *error1 = [request ERROR];
- if (!error1) {
- NSString *response = [request responsestring];
- NSLog (@"test:%@", response);
- }
- }
The first line of code snippet:
[CPP]View Plaincopy
- Nsdictionary *user = [[Nsdictionary alloc] initwithobjectsandkeys:@"0" @"Version", nil];
Constructs one of the simplest dictionary-type data, since iOS 5 provides an API to convert nsdictionary into JSON format.
The second line if it determines if the dictionary data can be JSON.
[CPP]View Plaincopy
- NSData *jsondata = [nsjsonserialization datawithjsonobject:user options:nsjsonwritingprettyprinted error: &error] ;
This is the way to convert nsdictionary into JSON format, where JSON-formatted data is stored in variables of type NSData.
[CPP]View Plaincopy
- Nsmutabledata *tempjsondata = [Nsmutabledata datawithdata:jsondata];
This sentence is to convert the NSData into Nsmutabledata, because the following we want to use ASIHTTPRequest to send JSON data, its message body must be stored in the Nsmutabledata format.
The following sentence to watch out
[CPP]View Plaincopy
- NSLog (@ "Register json:%@", [[NSString alloc] Initwithdata:tempjsondata encoding:nsutf8stringencoding]);
The main function is to record the data that was just JSON formatted
Below is the ASIHTTPRequest functional section:
[CPP]View Plaincopy
- Nsurl *url = [Nsurl urlwithstring:@"http://xxxx"];
- ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
The main function of these two sentences is to set the server-side address to interact with the client.
The next two sentences:
[CPP]View Plaincopy
- [Request addrequestheader:@"Content-type" value:@ "Application/json; Encoding=utf-8 "];
- [Request addrequestheader:@"Accept" value:@"Application/json"];
is the header information that sets the HTTP request information, from which you can see that the content type is JSON.
The next step is to set the request method (default to get) and the message body:
[CPP]View Plaincopy
- [Request setrequestmethod:@"POST"];
- [Request Setpostbody:tempjsondata];
After everything is set up, turn on the sync request:
[CPP]View Plaincopy
- [Request startsynchronous];
The last paragraph:
[CPP]View Plaincopy
- if (!error1) {
- NSString *response = [request responsestring];
- NSLog (@"rev:%@", response);
- }
Is the response information returned by the print server.
iOS submits JSON data via ASIHTTPRequest