【问题标题】:Post Request with AFHttpRequestOperationManager not working使用 AFHttpRequestOperationManager 发布请求不起作用
【发布时间】:2013-12-01 13:29:27
【问题描述】:

我正在使用 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}

我不知道这是什么问题。

【问题讨论】:

    标签: ios7 afnetworking-2


    【解决方案1】:

    在执行您的请求之前,您需要设置您的请求和响应序列化程序以使用 AFJSONRequestSerializer 和 AFJSONResponseSerializer 处理 JSON。为参数使用 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]);
                   }
    ];
    

    【讨论】:

    • 嗨,谢谢你的回复,我也试过了,但结果是一样的,这是我现在得到的响应{状态代码:415,标题{“缓存控制”=“最大-年龄=604800";连接=“保持活动”; “内容长度”= 0; “内容类型”=“应用程序/json”;日期 =“格林威治标准时间 2013 年 11 月 20 日星期三 06:52:24”;过期 =“格林威治标准时间 2013 年 11 月 27 日星期三 06:52:24”;服务器 = nginx; "X-Powered-By" = PleskLin; } }, NSLocalizedDescription=Request failed: unsupported media type (415), got 0},我不知道为什么内容长度为零。
    • 对我不起作用。传递给服务器的参数为空。
    【解决方案2】:

    只是解决我的问题,我的服务器没有配置为接受带有 application/json 的字符集 utf8,所以我只是删除了 Afnetworking 2.0 中 ajson 序列化程序的字符集 utf,现在它可以正常工作了。

    【讨论】:

    • 您能否更具体一点 - 您在哪里删除了哪些内容?
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多