responseObject不是JSON,而是AFHTTPRequestOperationManager的NSInLineData

时间:2014-11-30 05:29:19

标签: ios afnetworking-2

以下代码是通过operationQueue提交图像。请求都是正确地逐个触发,服务器响应包含客户端需要掌握的图像文件名。问题是成功/失败块的reponseObject不是预期解析的JSON,而是调试器中显示的NSInLineData类型。现在我怀疑从NSMutableURLRequest构造操作的代码导致了这个问题。请帮忙。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST"      
            URLString:podURLString parameters:nil
            constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

            NSError *error;
            BOOL success =[formData appendPartWithFileURL:imgURL name:@"images" fileName:img.path 
               mimeType:@"image/jpg" error:nil];
            if (!success)
                    NSLog(@"appendPartWithFileURL error: %@", error);} error:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   NSLog(@"Image Success: %@", [responseObject description]);

   NSString *imagePath = [response objectForKey:@"imageFileName"];

   [self.delegate networkManager:self didSubmitDeliveryImageForImageID:imagePath];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  NSLog(@"Image Error: %@", error);
  NSLog(@"image error: %@", [operation.responseObject description]);

 NSString *imageFilePath = [operation.responseObject objectForKey:@"imageFileName"];
 [self.delegate networkManager:self didFailSubmitDeliveryImageForImageID:imageFilePath];

}];
[manager.operationQueue addOperation:operation];

2 个答案:

答案 0 :(得分:6)

当您以NSInLineData获得响应时。现在走吧真好。如果它支持json格式,你可以在下面写一行代码来获取NSDictionary。

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObjec options:0 error:nil];

答案 1 :(得分:2)

只需在AFHTTPRequestOperation

之前添加此行代码即可
**operation.responseSerializer = [AFJSONResponseSerializer serializer];**