iPhone开发应用ASIFormDataRequest POST操作架构设计

来源:互联网 发布:sendto python buffer 编辑:程序博客网 时间:2024/06/17 20:16

from: http://mobile.51cto.com/iphone-283111.htm


iPhone开发应用中ASIFormDataRequest POST操作架构设计是本文哟啊介绍的内容,主要是来讲述ASIFormDataRequest的POST操作架构设计,方法由自己定义。内容主要是基于代码来实现,来看详细代码。

  1. //开启iphone网络开关  
  2. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;  
  3. ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURLURLWithString:host]];  
  4. //超时时间  
  5. request.timeOutSeconds = 30;  
  6. //定义异步方法  
  7. [request setDelegate:self];  
  8. [request setDidFailSelector:@selector(requestDidFailed:)];  
  9. [request setDidFinishSelector:@selector(requestDidSuccess:)];  
  10.  
  11. //用户自定义数据   字典类型  (可选)  
  12. request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"];  
  13. //post的数据  
  14. [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];  
  15. //开始执行  
  16. [request startAsynchronous];  
  17. //执行成功  
  18. - (void)requestDidSuccess:(ASIFormDataRequest *)request  
  19. {  
  20. //获取头文件  
  21. NSDictionary *headers = [request responseHeaders];  
  22. //获取http协议执行代码  
  23. NSLog(@"Code:%d",[request responseStatusCode]);  
  24. if ([delegaterespondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])  
  25. {  
  26. //执行委托操作  (架构设计   自选)  
  27. [delegate OARequestSuccessed:method withResponse:[request responseString]WithData:[request responseData] withHeaders:headers];  
  28. }  
  29. //清空  
  30. if (request)  
  31. {  
  32. [request release];  
  33. }  
  34. //关闭网络  
  35. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
  36. }  
  37. //执行失败  
  38. - (void)requestDidFailed:(ASIFormDataRequest *)request{  
  39.  
  40. //获取的用户自定义内容  
  41.  
  42. NSString *method = [request.userInfo objectForKey:@"Method"];  
  43. //获取错误数据  
  44. NSError *error = [request error];  
  45. if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)])   
  46. {  
  47. //执行委托 将错误数据传其他方式(架构设计   自选)  
  48. [delegate OARequestFailed:method withError:error];  
  49. }  
  50. if (request)   
  51. {  
  52. [request release];  
  53. }  
  54. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
  55. }  
  56.  
  57. //执行成功函数  
  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. //服务返回post后的数据  
  63. NSLog(@"response:\n%@",responseStr);  
  64. }  
  65.  
  66. //执行失败函数  
  67. - (void)OARequestFailed:(NSString *)method withError:(NSError *)error  
  68. {  
  69. NSLog(@"Error:%@",error);  
  70. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败, 请稍后重试." 
  71. delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];  
  72. [alert show];  
  73. [alert release];  
  74. }  

小结:iPhone开发应用中ASIFormDataRequest POST操作架构设计的内容介绍完了,希望通过本文的学习能对你有所帮助!