gpt4 book ai didi

ios7 - 使用 AFHttpRequestOperationManager 发布请求不起作用

转载 作者:行者123 更新时间:2023-12-03 21:17:52 25 4
gpt4 key购买 nike

我正在使用 AFHTTPRequestOperationManager 将一些 JSON 发布到我的服务器,我的代码如下。

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john", @"name", @"xxxxx@gmail.com", @"email", @"xxxx", @"password", @"1", @"type", nil];
// Do any additional setup after loading the view.
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];

[operationManager POST:@"posturl here" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
}];

响应如下:
2013-11-18 16:49:29.780 SwapOnceApiTester[12651:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unsupported media type (415), got 1664256" UserInfo=0x1565a6c0 {NSErrorFailingURLKey=xxxxxxx, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x15656db0> { URL: xxxxxxxxx } { status code: 415, headers {
"Cache-Control" = "max-age=604800";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Mon, 18 Nov 2013 11:49:28 GMT";
Expires = "Mon, 25 Nov 2013 11:49:28 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
"X-Powered-By" = PleskLin;
} }, NSLocalizedDescription=Request failed: unsupported media type (415), got 1664256}

我不知道这有什么问题。

最佳答案

您需要设置请求和响应序列化程序以使用 AFJSONRequestSerializer 处理 JSON和 AFJSONResponseSerializer在执行您的请求之前。使用 NSDictionary 文字作为参数也有助于代码清晰:

AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];

[operationManager POST:@"posturl here"
parameters: @{@"name": @"john",
@"email": @"xxxxx@gmail.com",
@"password: @"xxxxx",
@"type": @"1"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", [responseObject description]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error description]);
}
];

关于ios7 - 使用 AFHttpRequestOperationManager 发布请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com