IOS network programming synchronizes GET Method Request programming, and ios network programming synchronizes get
The iOS SDK provides two different APIs for synchronous and asynchronous requests for HTTP requests. You can also use GET or POST methods. First, we will understand the simplest synchronous GET request.
First, the query service can be implemented in the MasterViewController class of the master View Controller. 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 the data list
- @ Property (nonatomic, strong) NSMutableArray * listData;
- // Reload the table View
- -(Void) reloadView :( NSDictionary *) res;
- // Start requesting Web Service
- -(Void) startRequest;
- @ End
The introduction of the header file NSString + URLEncoding. H requires URL encoding in the program. Introducing the header file NSNumber + Message. h is used to convert the code returned by the server into messages that you can understand.
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;
- }
The code line ① [self startRequest] calls its own method startRequest to request Web Service. The startRequest method code in MasterViewController. m is as follows:
Java code
- /*
-
- * Start to request Web Service
-
- */
- -(Void) startRequest
- {
- NSString * strURL = [[NSString alloc] initWithFormat:
- @ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",
- @ "<Your iosbook1.com user email>", @ "JSON", @ "query"]; ①
- NSURL * url = [NSURL URLWithString: [strURL URLEncodedString]; ②
- NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url]; ③
- NSData * data = [NSURLConnection sendSynchronousRequest: request
- ReturningResponse: nil error: nil]; ④
- NSLog (@ "request completed ...");
- NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data
- Options: NSJSONReadingAllowFragments error: nil];
- [Self reloadView: resDict]; ⑤
- }
In addition, we also mentioned a classification NSString (URLEncoding). Its function 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 CFURLCreateStringByAddingPercentEscape function is a C function provided by the Core Foundation framework. It can convert the content into URL encoding. The line ② parameter specifies the character set that does not encode the invalid URL character, for example, "! . The line ③ parameter combines the character sets that must be encoded as valid URL characters.
Line ③ CFURLCreateStringByReplacingPercentEscapesUsingEncoding is a C function provided by the Core Foundation framework. It is opposite to the above CFURLCreateStringByAddingPercentEscape function and implements URL Decoding. The parameter in line ④ specifies the character set that is not decoded.
The Foundation framework also provides the Objective-C-based method for URL encoding and decoding. The NSString method corresponding to the CFURLCreateStringByAddingPercentEscape function is stringByAddingPercentEscapesUsingEncoding. The NSString method corresponding to the CFURLCreateStringByReplacingPercentEscapesUsingEncoding function is stringByReplacingPercentEscapesUsingEncoding. Since these methods cannot customize whether to encode or decode character sets, there is no flexibility in the above function.
How does iOS implement network programming?
You can use the CFNetwork framework and CFSocket socket to create UDP and TCP connections, send data, and set callback functions.
In IOS network programming, how to build server-side interaction with the client
Http can use the asihttprequest library, and tcp can use AsyncSocket. If http is used, the server may want to write services to accept http requests. If tcp is used, the server listens to the corresponding port for sending requests.