AFNetworking 3.0无法读取数据,因为格式不正确 [英] AFNetworking 3.0 The data couldn’t be read because it isn’t in the correct format

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

问题描述

还有其他标题相似的问题,但没有一个对我有帮助。我必须向服务器发送一个 PUT 请求,以更改约会状态,因此我将这种方法设置为-(void)appointmentStatusChangedTo: (NSString *)statusID atAppointmentID:(NSString *)appointmentID 我在其中将URL和参数设置为

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

然后我已发出URL请求,

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

所以最后我将其称为

[[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.

由于我没有与后端人员联系,因此目前不确定响应会是什么样。但据我所知,这只是一个简单的1。所以,请告诉我如何使用AFNetworking 3.0或代码中的任何更改来处理任何类型的响应。

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.

推荐答案

尝试使用以下代码:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

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

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

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

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