在使用AFNetWorking时,遇到了-1016问题,这个错误是因为 AFNetWorking框架的content-type默认只支持:application/json, text/json, text/javascript。
所以要在AFURLResponseSerialization.m中的 self.acceptableContentTypes 中加入所需的格式。
一般,这样就解决了-1016的错误,但是有时候还会遇到3840的错误:Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
这个错误是编码的问题,加入下面的代码即可:
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager是 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
但是这样做,新的问题就来了,下面这个方法
- (AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(id)parameters
constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
接收到responseObject的数据打印结果为:
<3030302c 22736571 223a307d 5d2c226c 696b6564 223a302c 22666176 6f726974 6564223a 302c2270 6f737473 4964223a 32353739 307d7d7d>
这是因为接收到的数据是没有解析。解析的代码为:
NSData *data = responseObject;
id jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
这里拿Json数据解析为例。这样问题就解决了。