Ios7 AFHttpRequestOperationManager不工作的Post请求

Ios7 AFHttpRequestOperationManager不工作的Post请求,ios7,afnetworking-2,Ios7,Afnetworking 2,我正在使用AFHTTPRequestOperationManager向我的服务器发布一些JSON,我的代码如下 NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john", @"name", @"xxxxx@gmail.com", @"email", @"xxxx", @"password", @"1", @"type", nil]; // Do any additional setup afte

我正在使用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}
2013-11-18 16:49:29.780 Swaponceapiter[12651:60b]错误:错误域=AFNetworkingErrorDomain code=-1011“请求失败:不支持的媒体类型(415),获取1664256”用户信息=0x1565a6c0{nserrorfailingurkey=xxxxxxx,afnetworkingOperationfailingurresponseerrorkey={URL:xxxxxxxxxxxxx}{状态代码:415,标头{
“缓存控制”=“最大使用年限=604800”;
连接=“保持活动”;
“内容类型”=“应用程序/json”;
日期=“2013年11月18日星期一11:49:28 GMT”;
Expires=“2013年11月25日星期一11:49:28 GMT”;
服务器=nginx;
“传输编码”=标识;
“X-Powered-By”=普莱斯克林;
}},NSLocalizedDescription=请求失败:不支持的媒体类型(415),获取1664256}

我不知道这有什么问题。

在执行请求之前,需要使用
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]);
               }
];

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

您好,感谢您的回复,我也尝试过,但结果相同,这就是我现在得到的响应{状态代码:415,标题{“缓存控制”=“max age=604800”;连接=“保持活动状态”;“内容长度”=0;“内容类型”=“application/json”;日期=“Wed,2013年11月20日06:52:24 GMT”;Expires=“Wed,2013年11月27日06:52:24 GMT”;服务器=nginx;“X-Powered-By”=PleskLin;},NSLocalizedDescription=请求失败:不支持的媒体类型(415),获取0},我不知道为什么内容长度为零。对我不起作用。传递给服务器的参数为空。您能更具体地说一下吗?您从何处删除了什么?