IOS network programming synchronizes GET Method Request programming, and ios network programming synchronizes get

Source: Internet
Author: User

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  
  1. # Import <UIKit/UIKit. h>
  2. # Import "NSString + URLEncoding. h"
  3. # Import "NSNumber + Message. h"
  4. @ Interface MasterViewController: UITableViewController
  5. @ Property (strong, nonatomic) DetailViewController * detailViewController;
  6. // Save the data list
  7. @ Property (nonatomic, strong) NSMutableArray * listData;
  8. // Reload the table View
  9. -(Void) reloadView :( NSDictionary *) res;
  10. // Start requesting Web Service
  11. -(Void) startRequest;
  12. @ 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  
  1. The main code in MasterViewController. m is as follows:
  2. -(Void) viewDidLoad
  3. {
  4. [Super viewDidLoad];
  5. Self. navigationItem. leftBarButtonItem = self. editButtonItem;
  6. Self. detailViewController = (DetailViewController *)
  7. [[Self. splitViewController. viewControllers lastObject] topViewController];
  8. [Self startRequest]; ①
  9. }
  10. # Pragma mark-Table View
  11. -(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
  12. Return 1;
  13. }
  14. -(NSInteger) tableView :( UITableView *) tableView
  15. NumberOfRowsInSection :( NSInteger) section {
  16. Return self. listData. count;
  17. }
  18. -(UITableViewCell *) tableView :( UITableView *) tableView
  19. CellForRowAtIndexPath :( NSIndexPath *) indexPath {
  20. UITableViewCell * cell
  21. = [TableView dequeueReusableCellWithIdentifier: @ "Cell"
  22. ForIndexPath: indexPath];
  23. NSMutableDictionary * dict = self. listData [indexPath. row];
  24. Cell. textLabel. text = [dict objectForKey: @ "Content"];
  25. Cell. detailTextLabel. text = [dict objectForKey: @ "CDate"];
  26. Return cell;
  27. }

 

 

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  
  1. /*
  2.  
  3. * Start to request Web Service
  4.  
  5. */
  6. -(Void) startRequest
  7. {
  8. NSString * strURL = [[NSString alloc] initWithFormat:
  9. @ "Http: // iosbook3/mynotes/webservice. php? Email =%@ & type =%@ & action =% @",
  10. @ "<Your iosbook1.com user email>", @ "JSON", @ "query"]; ①
  11. NSURL * url = [NSURL URLWithString: [strURL URLEncodedString]; ②
  12. NSURLRequest * request = [[NSURLRequest alloc] initWithURL: url]; ③
  13. NSData * data = [NSURLConnection sendSynchronousRequest: request
  14. ReturningResponse: nil error: nil]; ④
  15. NSLog (@ "request completed ...");
  16. NSDictionary * resDict = [NSJSONSerialization JSONObjectWithData: data
  17. Options: NSJSONReadingAllowFragments error: nil];
  18. [Self reloadView: resDict]; ⑤
  19. }

 

 

 

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  
  1. @ Interface NSString (URLEncoding)
  2. -(NSString *) URLEncodedString;
  3. -(NSString *) URLDecodedString;
  4. @ End
  5. @ Implementation NSString (URLEncoding)
  6. -(NSString *) URLEncodedString
  7. {
  8. NSString * result = (NSString *)
  9. CFBridgingRelease (CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, ①
  10. (CFStringRef) self,
  11. NULL, ②
  12. CFSTR ("+ $, # []"), ③
  13. KCFStringEncodingUTF8 ));
  14. Return result;
  15. }
  16. -(NSString *) URLDecodedString
  17. {
  18. NSString * result = (NSString *)
  19. CFBridgingRelease (CFURLCreateStringByReplacingPercentEscapesUsingEncoding
  20. (KCFAllocatorDefault, ③
  21. (CFStringRef) self, CFSTR (""), ④
  22. KCFStringEncodingUTF8 ));
  23. Return result;
  24. }
  25. @ 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.
 

Related Article

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.