AFNetworking 3.0 The data couldn’t be read because it isn’t in the correct format

耗尽温柔 提交于 2019-12-07 05:35:49

问题


There are other questions with similar titles but none of them helped me. I've to send a PUT request to server in order to change the status of appointment so I've made this method -(void)appointmentStatusChangedTo:(NSString *)statusID atAppointmentID:(NSString *)appointmentID In which I'm setting the URL and Parameters as

NSString *string = [NSString stringWithFormat:@"%@/API/Appointments/3",BaseURLString];
    NSDictionary *para = @{
                           @"AppointmentStatusId":statusID,
                           @"ID":appointmentID
                           };

Then I've made URL request as

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];

After that I'm setting the header for an authorization token as

 NSString *token = [NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userToken"]];

    [req setValue:token forHTTPHeaderField:@"Authorization"];

So finally I'm calling it as

[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error){
        if (!error) {
            if (response) {
                NSLog(@"Respose Object: %@",responseObject);
                [self.patientsAppointmentsTableView reloadData];
            }
        }
        else {
    // NSLog(@"Error: %@, %@, %@", error, response, responseObject);
    NSLog(@"Error: %@", error.localizedDescription);
}
        }] resume];

Now it is successfully sending the data to the server but as a response I'm getting

Error: The data couldn’t be read because it isn’t in the correct format.

I am not sure what the response might look like at the moment as I'm not in contact with backend guy. But as far as I remember it was just a simple 1. SO kindly tell me how to handle any type of response using AFNetworking 3.0 or any change in my code.


回答1:


try to use below code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

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

manager.requestSerializer=serializer;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];



回答2:


Try following code using Afnetworking 3.0

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        self.responseHandlers(YES,responseObject);
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        self.responseHandlers(NO,nil);
    }];


来源:https://stackoverflow.com/questions/39918440/afnetworking-3-0-the-data-couldn-t-be-read-because-it-isn-t-in-the-correct-forma

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