user2931321 user2931321 - 10 months ago 96
Objective-C Question

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

Request image from postman[![response image from postman]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.

Answer Source

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