还有其他问题有类似的标题,但没有一个帮助我.我要向服务器发送一个PUT请求,以便更改约会的状态,所以我做了这个方法 – (void)appointmentStatusChangedTo:(NSString *)statusID atAppointmentID:(NSString *)appointmentID我在其中设置了URL和参数为
NSString *string = [NSString stringWithFormat:@"%@/API/Appointments/3",BaseURLString];
NSDictionary *para = @{
@"AppointmentStatusId":statusID,
@"ID":appointmentID
};
然后我把URL请求作为
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"PUT" URLString:string parameters:para error:nil];
之后,我将授权令牌的标头设置为
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];
现在它成功地将数据发送到服务器,但作为我得到的响应
Error: The data couldn’t be read because it isn’t in the correct
format.
我不确定此刻的回应是什么样的,因为我没有与后端人接触.但据我记得,它只是一个简单的1.请告诉我如何使用AFNetworking 3.0处理任何类型的响应或我的代码中的任何更改.
最佳答案 尝试使用以下代码:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setStringEncoding:NSUTF8StringEncoding];
manager.requestSerializer=serializer;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];