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. request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"];
  12. //post的数据

  13. [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

  14. //开始执行

  15. [request startAsynchronous];
  16. //执行成功
复制代码

 

  1. - (void)requestDidSuccess:(ASIFormDataRequest *)request
  2. {
  3. //获取头文件
  4. NSDictionary *headers = [request responseHeaders];

  5. //获取http协议执行代码
  6. NSLog(@"Code:%d",[request responseStatusCode]);

  7. if ([delegaterespondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])
  8. {
  9. //执行委托操作  (架构设计   自选)
  10. [delegate OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];

  11. }
  12. //清空
  13. if (request)
  14. {
  15. [request release];
  16. }

  17. //关闭网络
  18. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


  19. //执行失败
复制代码


  1. - (void)requestDidFailed:(ASIFormDataRequest *)request{
  2. //获取的用户自定义内容
  3. NSString *method = [request.userInfo objectForKey:@"Method"];
  4. //获取错误数据
  5. NSError *error = [request error];

  6. if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)]) 
  7. {
  8. //执行委托 将错误数据传其他方式(架构设计   自选)
  9. [delegate OARequestFailed:method withError:error];
  10. }

  11. if (request) 
  12. {
  13. [request release];
  14. }

  15. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  16. }
  17. //执行成功函数

  18. - (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers
  19. {
  20. NSString *responseStr = [[[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding] autorelease];
  21. //服务返回post后的数据
  22. NSLog(@"response:\n%@",responseStr);
  23. }
  24. //执行失败函数

  25. - (void)OARequestFailed:(NSString *)method withError:(NSError *)error
  26. {ASIFormDataRequest的POST操作架构设计,方法由自己定义。

  27. //开启iphone网络开关
  28. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

  29. ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURLURLWithString:host]];
  30. //超时时间

  31. request.timeOutSeconds = 30;

  32. //定义异步方法

  33. [request setDelegate:self];
  34. [request setDidFailSelector:@selector(requestDidFailed:)];
  35. [request setDidFinishSelector:@selector(requestDidSuccess:)];

  36. //用户自定义数据   字典类型  (可选)
  37. request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"];
  38. //post的数据

  39. [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];

  40. //开始执行

  41. [request startAsynchronous];
  42. //执行成功

  43. - (void)requestDidSuccess:(ASIFormDataRequest *)request
  44. {
  45. //获取头文件
  46. NSDictionary *headers = [request responseHeaders];

  47. //获取http协议执行代码
  48. NSLog(@"Code:%d",[request responseStatusCode]);

  49. if ([delegaterespondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])
  50. {
  51. //执行委托操作  (架构设计   自选)
  52. [delegate OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];

  53. }
  54. //清空
  55. if (request)
  56. {
  57. [request release];
  58. }

  59. //关闭网络
  60. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;


  61. //执行失败

  62. - (void)requestDidFailed:(ASIFormDataRequest *)request{
  63. //获取的用户自定义内容
  64. NSString *method = [request.userInfo objectForKey:@"Method"];
  65. //获取错误数据
  66. NSError *error = [request error];

  67. if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)]) 
  68. {
  69. //执行委托 将错误数据传其他方式(架构设计   自选)
  70. [delegate OARequestFailed:method withError:error];
  71. }

  72. if (request) 
  73. {
  74. [request release];
  75. }

  76. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  77. }
  78. //执行成功函数

  79. - (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers
  80. {
  81. NSString *responseStr = [[[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding] autorelease];
  82. //服务返回post后的数据
  83. NSLog(@"response:\n%@",responseStr);
  84. }
  85. //执行失败函数

  86. - (void)OARequestFailed:(NSString *)method withError:(NSError *)error
  87. {
  88. NSLog(@"Error:%@",error);

  89. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败, 请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
  90. [alert show];
  91. [alert release];
  92. }
  93. NSLog(@"Error:%@",error);

  94. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败, 请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
  95. [alert show];
  96. [alert release];
  97. }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值