我正在使用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}
我不知道这个问题是什么。
答案 0 :(得分:29)
在执行请求之前,您需要设置请求和响应序列化程序以使用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]);
}
];
答案 1 :(得分:0)
解决我的问题,我的服务器没有配置为使用application / json接受charset utf8所以我刚刚删除了Afnetworking 2.0中ajson序列化程序的charset utf,现在它正在运行中。