Design of the POST operation architecture of the iPhone development application ASIFormDataRequest

Source: Internet
Author: User

IPhoneDeveloping applicationsASIFormDataRequestPOST OperationArchitectureDesign is the content of this article.ASIFormDataRequestPOST OperationArchitectureDesign, methods defined by yourself. The content is mainly implemented based on code. Let's look at the detailed code.

 
 
  1. // Enable the iphone network switch
  2. [UIApplication sharedApplication]. networkActivityIndicatorVisible = YES;
  3. ASIFormDataRequest * request = [[ASIFormDataRequest alloc] initWithURL: [NSURLURLWithString: host];
  4. // Timeout
  5. Request. timeOutSeconds = 30;
  6. // Define the Asynchronous Method
  7. [Request setDelegate: self];
  8. [Request setDidFailSelector: @ selector (requestDidFailed :)];
  9. [Request setDidFinishSelector: @ selector (requestDidSuccess :)];
  10.  
  11. // User-defined data dictionary type (optional)
  12. Request. userInfo = [NSDictionary dictionaryWithObject: method forKey: @ "Method"];
  13. // Post Data
  14. [Request appendPostData: [body dataUsingEncoding: NSUTF8StringEncoding];
  15. // Start execution
  16. [Request startAsynchronous];
  17. // Execution successful
  18. -(Void) requestDidSuccess :( ASIFormDataRequest *) request
  19. {
  20. // Obtain the header file
  21. NSDictionary * headers = [request responseHeaders];
  22. // Obtain the http protocol Execution Code
  23. NSLog (@ "Code: % d", [request responseStatusCode]);
  24. If ([delegaterespondsToSelector: @ selector (OARequestSuccessed: withResponse: WithData: withHeaders :)])
  25. {
  26. // Select the operating architecture to be delegated)
  27. [Delegate OARequestSuccessed: method withResponse: [request responseString] WithData: [request responseData] withHeaders: headers];
  28. }
  29. // Clear
  30. If (request)
  31. {
  32. [Request release];
  33. }
  34. // Disable the network
  35. [UIApplication sharedApplication]. networkActivityIndicatorVisible = NO;
  36. }
  37. // Execution failed
  38. -(Void) requestDidFailed :( ASIFormDataRequest *) request {
  39.  
  40. // Obtain the User-Defined content
  41.  
  42. NSString * method = [request. userInfo objectForKey: @ "Method"];
  43. // Get Error Data
  44. NSError * error = [request error];
  45. If ([delegate respondsToSelector: @ selector (OARequestFailed: withError :)])
  46. {
  47. // Execute the delegate to transfer the wrong data to other methods. The architecture design is optional)
  48. [Delegate OARequestFailed: method withError: error];
  49. }
  50. If (request)
  51. {
  52. [Request release];
  53. }
  54. [UIApplication sharedApplication]. networkActivityIndicatorVisible = NO;
  55. }
  56.  
  57. // Function for successful execution
  58.  
  59. -(Void) OARequestSuccessed :( NSString *) method withResponse :( NSString *) response WithData :( NSData *) data withHeaders :( NSDictionary *) headers
  60. {
  61. NSString * responseStr = [[NSString alloc] initWithData: dataencoding: NSUTF8StringEncoding] autorelease];
  62. // The service returns post Data
  63. NSLog (@ "response: \ n % @", responseStr );
  64. }
  65.  
  66. // Execution failure Function
  67. -(Void) OARequestFailed :( NSString *) method withError :( NSError *) error
  68. {
  69. NSLog (@ "Error: % @", error );
  70. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "error" message: @ "network connection failed. Please try again later ."
  71. Delegate: nil cancelButtonTitle: @ "good" otherButtonTitles: nil];
  72. [Alert show];
  73. [Alert release];
  74. }

Summary:IPhoneDeveloping applicationsASIFormDataRequestPOST OperationArchitectureThe content of the design has been introduced. I hope this article will help you!

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.