iOS network programming synchronous get method request programming

Source: Internet
Author: User

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
  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 a list of data
  7. @property (Nonatomic,strong) nsmutablearray* listData;
  8. Reload Table View
  9. -(void) Reloadview: (nsdictionary*) Res;
  10. Start requesting a Web Service
  11. -(void) startrequest;
  12. @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
  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. }

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
  1. /*
  2. * Start requesting web Service
  3. */
  4. -(void) Startrequest
  5. {
  6. NSString *strurl = [[NSString alloc] Initwithformat:
  7. @ "http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@",
  8. @ "< your iosbook1.com user mailbox >" @ "JSON" @ "Query"]; ①
  9. Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]; Ii
  10. Nsurlrequest *request = [[Nsurlrequest alloc] initwithurl:url]; ③
  11. NSData *data = [Nsurlconnection sendsynchronousrequest:request
  12. Returningresponse:nil Error:nil]; ④
  13. NSLog (@ "request complete ...");
  14. Nsdictionary *resdict = [Nsjsonserialization jsonobjectwithdata:data
  15. Options:nsjsonreadingallowfragments Error:nil];
  16. [Self reloadview:resdict]; ⑤
  17. }

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
  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 ① 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.

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.