使用AFNetworking上传图片遇到的问题

来源:互联网 发布:c 的sms源码 编辑:程序博客网 时间:2024/04/29 19:00

1. The data couldn’t be read because it isn’t in the correct format

最近一个项目,在请求接口时,一直有问题,使用get请求可以正常请求到数据,而post则无法获取数据

控制台输出:

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. http://192.168.69.121:8080/artboss-webapp/ios/checkcode  
  2.  params:{  
  3.     "user_phonenumber" = 185****0925;  
  4. }  
  5.  errorInfos:The data couldn’t be read because it isn’t in the correct format.  
大意就是数据格式不对,无法读取!后台也接收不到我传递的参数;

使用Charles抓包会发现是这样的信息:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. HTTP Status 400 - Request String parameter 'user_phonenumber' is not present  

400 Bad Request!



我一看是非法的网络请求,以为是我的问题,就一直在我这边找原因,最后才发现,这是因为我发送请求时发送的字符串,和后台需要的字符串格式不一致,发送请求时,后台需要的是text文本格式,而我发送的是json格式,导致后台无法识别,接收不到数据!

后来发现我所用的封装类里的请求头,有如下设置:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];  
  2. [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];  
即,默认是使用json传输数据的!

把第二个注释掉,即使用默认的Content-Type,

并在发送请求时设置发送的字段为文本格式,即:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. manager.requestSerializer = [AFHTTPRequestSerializer serializer];  
本以为会解决问题,但事实还是请求不到数据!!

最后不得已使用原生的AFNetworking进行测试,post正常拿到了数据,后来对比两者的区别,除了上面的改动以外,请求返回的数据也要改为文本,即:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. manager.responseSerializer = [AFHTTPResponseSerializer serializer];  


这样才正常拿到了数据!!

总之,此次网络请求的异常,是由于请求和响应的文本格式没有统一,导致参数无法正常传递,数据无法正常读取!不过,竟然不知json传输,我也是醉了...


2. Invalid parameter not satisfying: body


在使用AFNetworking进行传图操作的时候,出现了这个crash信息:

模拟器运行后控制台输出:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason'Invalid parameter not satisfying: body'  
  2. *** First throw call stack:  
  3. (  
  4.     0   CoreFoundation                      0x0000000107ad4e65 __exceptionPreprocess + 165  
  5.     1   libobjc.A.dylib                     0x0000000106c1edeb objc_exception_throw + 48  
  6.     2   CoreFoundation                      0x0000000107ad4cca +[NSException raise:format:arguments:] + 106  
  7.     3   Foundation                          0x000000010431d4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198  
  8. )  
  9. libc++abi.dylib: terminating with uncaught exception of type NSException  

真机的话会输出如下信息:
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason'Invalid parameter not satisfying: body'  
  2. *** First throw call stack:  
  3. (0x225e62eb 0x21db2dff 0x225e61c1 0x22dbcd30x1b8d30x1b8981 0x1909f1 0x1b4a60x1a1be9 0x1906c1 0x176535 0x177171 0x193371 0x18f5cb 0x19fbb9 0x8dfcbf 0x8dfcab 0x8e4771 0x225a8fc5 0x225a74bf 0x224f9bb9 0x224f99ad 0x23773af9 0x267e5fb5 0x118b11 0x221ac873)  
  4. libc++abi.dylib: terminating with uncaught exception of type NSException  
  5. (lldb)   

而且crash的地方也不一样:

模拟器crash到自动释放池



真机的话carsh到AFN的底层:



其实,最主要的信息就是:Invalid parameter not satisfying: body (无效的参数:body)

问题就出在body这个参数上,模拟器上看不出什么头绪,请求参数中也没有body这个参数;但是在真机上的crash信息可以看出一些头绪:他是crash到了这个方法里

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (void)appendPartWithHeaders:(NSDictionary *)headers  
  2.                          body:(NSData *)body  
  3. {  
  4.     NSParameterAssert(body);  
  5.   
  6.     AFHTTPBodyPart *bodyPart = [[AFHTTPBodyPart alloc] init];  
  7.     bodyPart.stringEncoding = self.stringEncoding;  
  8.     bodyPart.headers = headers;  
  9.     bodyPart.boundary = self.boundary;  
  10.     bodyPart.bodyContentLength = [body length];  
  11.     bodyPart.body = body;  
  12.   
  13.     [self.bodyStream appendHTTPBodyPart:bodyPart];  
  14. }  

可以看到body的类型是NSData,而设置的请求参数中,只有要发送的照片数据是NSData类型,是不是照片的问题呢?进到AFN的底层可以发现,AFN上传图片主要是用到了这个方法:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (void)appendPartWithFileData:(NSData *)data  
  2.                           name:(NSString *)name  
  3.                       fileName:(NSString *)fileName  
  4.                       mimeType:(NSString *)mimeType  
  5. {  
  6.     NSParameterAssert(name);  
  7.     NSParameterAssert(fileName);  
  8.     NSParameterAssert(mimeType);  
  9.   
  10.     NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];  
  11.     [mutableHeaders setValue:[NSString stringWithFormat:@"form-data; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];  
  12.     [mutableHeaders setValue:mimeType forKey:@"Content-Type"];  
  13.   
  14.     [self appendPartWithHeaders:mutableHeaders body:data];  
  15. }  

在这个方法里调用了

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [self appendPartWithHeaders:mutableHeaders body:data];  

正是,程序crash的地方;

其实,打断点调试后也能发现传入的照片数据为nil,问题的根源找到了,问题也就解决了!!!


以上内容转自 http://blog.csdn.net/lqq200912408/article/details/50520631

0 0
原创粉丝点击