ios7 – 发送请求与AFHttpRequestOperationManager不工作

前端之家收集整理的这篇文章主要介绍了ios7 – 发送请求与AFHttpRequestOperationManager不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用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和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]);
               }
];

猜你在找的iOS相关文章