AFNetworking版本2内容类型错误

时间:2014-01-23 22:51:27

标签: ios objective-c afnetworking afnetworking-2 afjsonrequestoperation

我正在尝试制作一款与特定JIRA服务器互动的iPhone应用程序。我有以下代码登录:

NSURL *url = [[NSURL alloc] initWithString:@"https://mycompany.atlassian.net/rest/auth/latest/session/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
    ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"ERROR: %@", error);

    }
 ];
 [operation start];

但是它给了我以下与Content-Type有关的错误:

ERROR: Error Domain=AFNetworkingErrorDomain Code=-1011
"Request failed: unsupported media type (415)"
UserInfo=0x8cd6540
{
  NSErrorFailingURLKey=https://mycompany.atlassian.net/rest/auth/latest/session/,
  NSLocalizedDescription=Request failed: unsupported media type (415),
  NSUnderlyingError=0x8c72e70
  "Request failed: unacceptable content-type: text/html", 

我不确定问题是什么。我找到了 this question ,我认为这可能是一个类似的问题,但答案是要么使用AFJSONRequestOperation类(我不能,因为我正在使用AFNetworking版本2,没有那个类),或者在服务器端修复它(由于显而易见的原因,我也不能这样做)。

当我无法修复服务器端而无法使用AFJSONRequestOperation时,我可以修复此错误的哪些内容?

5 个答案:

答案 0 :(得分:14)

如果使用AFNetworking 2.0,您可以使用POST方法,这简化了这一点:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:@"https://mycompany.atlassian.net/rest/auth/latest/session/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这会创建请求,根据Content-Type设置设置requestSerializer,并为您编码JSON。 AFNetworking的一个优点是你可以摆脱手工构建和配置NSURLRequest对象的杂草。


顺便说一句,“请求失败:不可接受的内容类型:text / html”错误意味着无论您期望接收什么(例如JSON),您都会收到HTML响应。这很常见:许多服务器错误(例如服务器通知您请求格式错误等)会生成HTML错误消息。如果您想在failure块中看到该HTML,只需记录operation.responseString

答案 1 :(得分:2)

事实证明问题在于这一行:

[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];

应该是

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type" ];

现在解决了错误。 (编辑:我应该同时使用两者,请参阅CouchDeveloper的评论。)

修改
Rob的解决方案更好,所以我会继续使用它。我实际上尝试过类似于他所展示的解决方案,但是他有这条线,

manager.requestSerializer = [AFJSONRequestSerializer serializer];

我有这条线:

[manager.requestSerializer
    setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

......哪些不起作用。感谢Rob让它发挥作用!

答案 2 :(得分:1)

我在AFNetworking 2.0中遇到了同样的问题,解决方法是我必须确保设置AFHTTPRequestSerializer类型(如果您的请求是JSON),它应该是这样的。

AFHTTPSessionManager *myHTTPManager = ...
[myManager setRequestSerializer:[AFJSONRequestSerializer serializer]];

您必须同时设置AFHTTPResponseSerializerAFHTTPRequestSerializer

答案 3 :(得分:0)

如果您使用的是默认书面类 AFAppDotNetAPIClient ,则会遇到此错误。必须添加 responseSerializer 。看你的共享方法应该是 -

+ (instancetype)sharedClient {

static AFAppDotNetAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
    _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
    _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    _sharedClient.responseSerializer = [AFHTTPResponseSerializer serializer];
});
return _sharedClient;}

答案 4 :(得分:0)

使用此行代码。

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];