由于数据格式不正确,因此无法读取。” [英] The data couldn't be read because it isn't in the correct format."

查看:562
本文介绍了由于数据格式不正确,因此无法读取。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

] 2 我正在使用AFNetworking将数据发送到服务器。下面是我的代码:

]2I am using AFNetworking to send data to server.Below is my code:

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);

 }

以下是我得到的错误:

由于格式不正确而无法读取数据。

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

错误

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

Error:Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

如果我nslog error.debugdescription iam得到由于数据格式错误,无法读取数据 。

If I nslog error.debugdescription iam getting as "The data couldn't be read because it isn't in the correct format."

请帮助我解决此错误。

谢谢。

推荐答案

尝试以下代码:

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

In AFNetworking 3.0 AFHTTPRequestOperationManager is replaced with AFHTTPSessionManager.

迁移指南

这篇关于由于数据格式不正确,因此无法读取。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆