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

偶尔善良 提交于 2019-12-22 09:52:52

问题


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

 }

Below is the error I am getting:

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

Error:

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

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

Please help me to solve this error.

Thank you.


回答1:


Try below code:

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
     }];

In AFNetworking 3.0 AFHTTPRequestOperationManager is replaced with AFHTTPSessionManager.

Migration Guide




回答2:


actually this problem occurred mainly two reasons

  1. when your parameters are wrong. means wrong value or missing parameter.
  2. check your URL . sometime it occurred cause of your wrong URL.

i faced same problem two times with same reason.



来源:https://stackoverflow.com/questions/39740257/the-data-couldnt-be-read-because-it-isnt-in-the-correct-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!