AFNetworking如何获取完整的error body
来源:互联网 发布:青岛网络电视户外运动 编辑:程序博客网 时间:2024/05/16 06:58
发现很多人都遇到了同样的问题,当我们使用AFNetwoking请求失败的时候,我们想要获取一下失败的信息,系统只返回给我们一个status code,却不告诉我们具体原因。
我在这里简单的说一下应该如何进行操作才能得到完整的错误信息。
新建一个类名为
HMFJSONResponseSerializerWithData
我们在
HMFJSONResponseSerializerWithData.h中写上
#import "AFURLResponseSerialization.h"/// NSError userInfo keys that will contain response datastatic NSString * const JSONResponseSerializerWithDataKey = @"body";static NSString * const JSONResponseSerializerWithBodyKey = @"statusCode";@interface HMFJSONResponseSerializerWithData : AFJSONResponseSerializer@end
在HMFJSONResponseSerializerWithData.m中写上
#import "HMFJSONResponseSerializerWithData.h"@implementation HMFJSONResponseSerializerWithData- (id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error{ id JSONObject = [super responseObjectForResponse:response data:data error:error]; // may mutate `error` if (*error != nil) { NSMutableDictionary *userInfo = [(*error).userInfo mutableCopy]; [userInfo setValue:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:JSONResponseSerializerWithDataKey]; [userInfo setValue:[response valueForKey:JSONResponseSerializerWithBodyKey] forKey:JSONResponseSerializerWithBodyKey]; NSError *newError = [NSError errorWithDomain:(*error).domain code:(*error).code userInfo:userInfo]; (*error) = newError; } return JSONObject;}@end
然后在需要使用该方法的文件中先引用这个类
在做请求之前添加上如下代码
NSString * url = [NSStringstringWithFormat:@"%@agent/user",BaseUrl];
AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager
alloc] init];
manager.responseSerializer = [HMFJSONResponseSerializerWithDataserializer];
注* 我是根据一篇国外的文章写的。
0 0
- AFNetworking如何获取完整的error body
- AFNetWorking post body内容
- JS获取body的高度
- 认识DOM文档的遍历指针以及如何获取根节点和body节点
- Joomla里如何获取文章的完整URL地址
- win7系统如何获取文件或文件夹的完整路径
- JavaScript获取Body的宽、高度
- 获取body的长度和宽度
- Magento 获取当前页面body的class
- js获取KindEditor body里面的值
- 点击body获取点击的对象
- 获取通过body传输的postdata数据
- gin 获取post请求的json body
- ios 获取body标签的高度
- android Volley获取VolleyError的body
- 获取元素相对于body的位置
- 获取通过body传输的postdata数据
- beego 获取 Request Body 里的内容
- andoirdUI那些事(二)
- MFC六大关键技术之仿真学习笔记(二)
- 关于苹果的xcode和ios升级
- Qt界面显示图片,Qt与opencv的联合运用
- jquery实现百分比进度条
- AFNetworking如何获取完整的error body
- 小SQL干大事。。。不断更新
- Unicode和UTF-8的关系
- java中面向对象的一些小知识点(二)
- 泰兴身份证号码户籍地址信息查询
- spring IoC 优点和缺点
- 只有创意和杜蕾斯才能救校园O2O
- 简明扼要谈Spring IOC的好处
- 在struts2种解决url中文乱码的方法(其中一种方法)