ios7 – 发送请求与AFHttpRequestOperationManager不工作

我使用AFHTTPRequestOperationManager将一些 JSON发布到我的服务器,我的代码如下.
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:@"john",@"name",@"[email protected]",@"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":   @"[email protected]",@"password: @"xxxxx",@"type":    @"1"}
               success:^(AFHTTPRequestOperation *operation,id responSEObject) {
                   NSLog(@"JSON: %@",[responSEObject description]);
               } 
               failure:^(AFHTTPRequestOperation *operation,NSError *error) {
                   NSLog(@"Error: %@",[error description]);
               }
];

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...