[英]AFNetworking 2.0 : Cannot POST with JSON from IOS
Ever since upgrading my IOS code to use AFNetworking version 2.0 instead of 1.x, I cannot do an HTTP Post any more with JSON parameters. 自从升级我的IOS代码以使用AFNetworking版本2.0而不是1.x后,我无法再使用JSON参数执行HTTP Post。
I suspect it is because my HTTP request header is not "application/json" but I have tried to do that and it still does not work. 我怀疑这是因为我的HTTP请求标头不是“application / json”,但我试图这样做但它仍然无法正常工作。
This is my test code for trying to POST by JSON: 这是我尝试通过JSON POST的测试代码:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password",
nil];
[[OTApiClient sharedInstance] POST:@"login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"result: %@", responseObject);
Boolean response = [[responseObject valueForKey:@"success"] boolValue];
if(response == TRUE) {
//ok, success
NSLog(@"success");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Successfully logged in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
} else {
NSLog(@"Not successful");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"The email or password is incorrect." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@ , for operation: %@", error, operation);
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"There is a problem connecting to the server, please try again soon." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
}];
});
and I keep getting this error : 我一直收到这个错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0xca80730 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} , for operation: <AFHTTPRequestOperation: 0xca6bee0, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0xca76040> { URL: http://1.2.3.4:9000/api/login }, response: <NSHTTPURLResponse: 0xc963d60> { URL: http://1.2.3.4:9000/error404 } { status code: 404, headers {
"Content-Length" = 1809;
"Content-Type" = "text/html; charset=utf-8";
} }> }}>
I am very sure that the server is up and accessible because all other GET calls work properly. 我非常确定服务器已启动且可访问,因为所有其他GET调用都能正常工作。
I have checked the updated documentation for AFNetworking 2.0 and it seems like what I have is the right way to do a JSON POST 我已经检查了AFNetworking 2.0的更新文档,看起来我所拥有的是进行JSON POST的正确方法
Please let me know if I missed anything 如果我遗漏了什么,请告诉我
Thanks IS 谢谢你
Actually, I finally got it to work. 实际上,我终于开始工作了。
Basically, for my requestSerializer of the AFHTTPRequestOperationManager, I was using AFHTTPRequestSerializer and then trying to set the Content-Type to "application/json" 基本上,对于AFHTTPRequestOperationManager的requestSerializer,我使用的是AFHTTPRequestSerializer,然后尝试将Content-Type设置为“application / json”
But once I switched to using AFJSONRequestSerializer instead, it works fine now 但是一旦我转而使用AFJSONRequestSerializer,它现在工作正常
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
operationManagerInstance.requestSerializer = requestSerializer;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = serializer;
now it will work. 现在它会起作用。
I encountered the same problem using AFNetworking API through Swift, trying to send a post request and finally got it work as well thanks to user1805458's post. 我通过Swift使用AFNetworking API遇到了同样的问题,试图发送一个帖子请求,最后得到它的工作,多亏了user1805458的帖子。
Basically, for my requestSerializer of the AFHTTPRequestOperationManager, I was also using AFHTTPRequestSerializer and then trying to set the Content-Type to "Application/JSON" and it did not work. 基本上,对于AFHTTPRequestOperationManager的requestSerializer,我也使用AFHTTPRequestSerializer,然后尝试将Content-Type设置为“Application / JSON”,但它不起作用。
But once I switched to using AFJSONRequestSerializer instead, it works fine now: 但是一旦我转而使用AFJSONRequestSerializer,它现在工作正常:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: nil)
// Contrary to user1805458's solution, I did not need to use these two instructions below using Swift.
// manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Content-Type")
// manager.requestSerializer.setValue("Application/JSON", forHTTPHeaderField:"Accept")
I hope it will help you to fix this error in case where you use Swift, AFNetworking and try to send a post request containing a JSON body. 我希望它可以帮助您修复此错误,以防您使用Swift,AFNetworking并尝试发送包含JSON正文的帖子请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.