The IOS SDK provides synchronous and asynchronous requests for HTTP requests in two different APIs, and can use request methods such as Get or post. Let's start by understanding the simplest synchronous get method requests.
The first implementation of the query business, query business requests can be implemented in the main view controller Masterviewcontroller class, where the MasterViewController.h code is as follows:
Java code
- #Import <UIKit/UIKit.h>
- #Import "Nsstring+urlencoding.h"
- #Import "Nsnumber+message.h"
- @interface Masterviewcontroller:uitableviewcontroller
- @property (Strong, nonatomic) Detailviewcontroller *detailviewcontroller;
- Save a list of data
- @property (Nonatomic,strong) nsmutablearray* listData;
- Reload Table View
- -(void) Reloadview: (nsdictionary*) Res;
- Start requesting a Web Service
- -(void) startrequest;
- @end
The introduction of the header file Nsstring+urlencoding.h file is required to encode the URL in the program. The introduction of the header file Nsnumber+message.h file is the process of converting the server return message code into a message that the user can read.
Java code
- The main code in MASTERVIEWCONTROLLER.M is as follows:
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Self.navigationItem.leftBarButtonItem = Self.editbuttonitem;
- Self.detailviewcontroller = (Detailviewcontroller *)
- [[Self.splitViewController.viewControllers Lastobject] topviewcontroller];
- [Self startrequest]; ①
- }
- #pragma mark–table View
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
- return 1;
- }
- -(Nsinteger) TableView: (UITableView *) TableView
- Numberofrowsinsection: (nsinteger) Section {
- return self.listData.count;
- }
- -(UITableViewCell *) TableView: (UITableView *) TableView
- Cellforrowatindexpath: (Nsindexpath *) Indexpath {
- UITableViewCell *cell
- = [TableView dequeuereusablecellwithidentifier:@"Cell"
- Forindexpath:indexpath];
- nsmutabledictionary* dict = Self.listdata[indexpath.row];
- Cell.textLabel.text = [dict objectforkey:@"Content"];
- Cell.detailTextLabel.text = [dict objectforkey:@"CDate"];
- return cell;
- }
Where the ① line code [self startrequest] calls its own method Startrequest implements the request Web Service. The Startrequest method code in MASTERVIEWCONTROLLER.M is as follows:
Java code
- /*
- * Start requesting web Service
- */
- -(void) Startrequest
- {
- NSString *strurl = [[NSString alloc] Initwithformat:
- @ "http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@",
- @ "< your iosbook1.com user mailbox >" @ "JSON" @ "Query"]; ①
- Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]; Ii
- Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url]; ③
- NSData *data = [Nsurlconnection sendsynchronousrequest:request
- Returningresponse:nil Error:nil]; ④
- NSLog (@ "request complete ...");
- Nsdictionary *resdict = [Nsjsonserialization jsonobjectwithdata:data
- Options:nsjsonreadingallowfragments Error:nil];
- [Self reloadview:resdict]; ⑤
- }
In addition, we mentioned in the previous article A classification nsstring (urlencoding), its role is to encode and decode the URL, its code is as follows:
Java code
- @interface NSString (urlencoding)
- -(NSString *) urlencodedstring;
- -(NSString *) urldecodedstring;
- @end
- @implementation NSString (urlencoding)
- -(NSString *) urlencodedstring
- {
- NSString *result = (NSString *)
- Cfbridgingrelease (Cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault,①
- (Cfstringref) Self,
- Null,②
- Cfstr ("+$,#[]"), ③
- KCFSTRINGENCODINGUTF8));
- return result;
- }
- -(nsstring*) urldecodedstring
- {
- NSString *result = (NSString *)
- Cfbridgingrelease (cfurlcreatestringbyreplacingpercentescapesusingencoding
- (Kcfallocatordefault,③
- (Cfstringref) Self, Cfstr (""), ④
- KCFSTRINGENCODINGUTF8));
- return result;
- }
- @end
The ① Line code Cfurlcreatestringbyaddingpercentescape function is the C function provided by the core Foundation framework to convert the content into URL encoding. The ② line parameter specifies a character set that does not encode itself as an illegal URL character, such as a symbol such as "!* ()". The ③ line parameter is a character set that encodes itself as a legitimate URL character.
The ③ Line code cfurlcreatestringbyreplacingpercentescapesusingencoding function is the C function provided by the Core Foundation framework, It is the opposite of the above Cfurlcreatestringbyaddingpercentescape function, which is URL decoding. The parameter of line ④ specifies the character set that is not decoded.
The foundation framework also provides OBJECTIVE-C-based methods for URL encoding and decoding, and the NSString method corresponding to the Cfurlcreatestringbyaddingpercentescape function is Stringbyaddingpercentescapesusingencoding. The NSString method corresponding to the Cfurlcreatestringbyreplacingpercentescapesusingencoding function is Stringbyreplacingpercentescapesusingencoding: Because these methods cannot be customized to encode and decode the character set, there is no flexibility in the function above.