无法读取数据,因为格式不正确。“

时间:2016-09-28 07:04:04

标签: ios objective-c iphone nsdata afnetworking-3

Request image from postman[![response image from postman] 2我正在使用AFNetworking将数据发送到server.Below是我的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
    [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];


    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

    [dict setValue:[[arrLoginDetails objectAtIndex:0] objectForKey:@"id"] forKey:@"user_id"];

    [dict setValue:[[arrLoginDetails objectAtIndex:0] objectForKey:@"id"] forKey:@"shipper_id"];

    [dict setValue:strUuid forKey:@"imageOrderId"];

    [dict setValue:@"1" forKey:@"type"];

    NSData * imageData = UIImageJPEGRepresentation([aryImages objectAtIndex:0], 0.7);


   if([imageData length]>0){
    NSString *base64String = [imageData base64EncodedStringWithOptions:0];


    if([base64String length]>0){
    [dict setValue:base64String forKey:@"file_path"];

       NSString * reqFormat= [NSString stringWithFormat:@"%@&email=%@&dev_id=%@",UploadImage,[[arrLoginDetails objectAtIndex:0] objectForKey:@"email"],APP_DELEGATE.stringDeviceID];

 [manager POST:reqFormat parameters:dict1 success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
  }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"error:%@",error.debugDescription);

 }

以下是我得到的错误:

无法读取数据,因为格式不正确。

错误

错误:错误域= NSCocoaErrorDomain代码= 3840“没有值。” UserInfo = {NSDebugDescription =无值。}

如果我nslog error.debugdescription iam得到“数据无法读取,因为它的格式不正确。”

请帮我解决这个错误。

谢谢。

2 个答案:

答案 0 :(得分:5)

尝试以下代码:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];

manager.requestSerializer=serializer;
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html",@"application/json", nil];

 [manager POST:reqFormat parameters:dict1 progress:nil success:^(NSURLSessionTask *task, id responseObject)
     {
         //Print response
     } failure:^(NSURLSessionTask *operation, NSError *error) {
         //Print error
     }];

在AFNetworking 3.0 AFHTTPRequestOperationManager替换为AFHTTPSessionManager

Migration Guide

答案 1 :(得分:0)

实际上这个问题主要发生了两个原因

  1. 当你的参数错误时。意味着错误的值或缺少参数。
  2. 检查您的网址。有时会出现错误的网址。
  3. 我出于同样的原因两次遇到同样的问题。