206

我正在试用 AFNetworking 的新版本 2.0,但出现上述错误。知道为什么会这样吗?这是我的代码:

    NSURL *URL = [NSURL URLWithString:kJSONlink];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [[NSOperationQueue mainQueue] addOperation:op];

我正在使用 Xcode 5.0。

另外,这是错误消息:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xda2e670 {NSErrorFailingURLKey=kJSONlink, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda35180> { URL: kJSONlink } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 2898;
    "Content-Type" = "text/html";
    Date = "Tue, 01 Oct 2013 10:59:45 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = Apache;
    Vary = "Accept-Encoding";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

我只是使用 kJSONlink 隐藏了 JSON。这应该返回一个 JSON。

4

14 回答 14

363

这意味着您的服务器正在发送"text/html"而不是已经支持的类型。我的解决方案是添加"text/html"到课堂上acceptableContentTypesAFURLResponseSerialization只需搜索“acceptableContentTypes”并@"text/html"手动添加到集合中。

当然,理想的解决方案是更改从服务器发送的类型,但为此您必须与服务器团队交谈。

于 2013-10-01T16:21:35.260 回答
179

设置我的RequestOperationManager响应序列化程序来HTTPResponseSerializer解决这个问题。

Objective-C

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

迅速

manager.responseSerializer = AFHTTPResponseSerializer()

进行此更改意味着我不需要添加acceptableContentTypes到我提出的每个请求中。

于 2013-10-05T18:33:35.350 回答
73

我将@jaytrixz 的回答/评论更进一步,并将“text/html”添加到现有的一组类型中。这样,当他们在服务器端将其修复为“application/json”或“text/json”时,我声称它会无缝工作。

  manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
于 2014-02-15T22:54:36.973 回答
33

在服务器端,我添加了:

header('Content-type: application/json');

进入我的 .php 代码,这也解决了问题。

于 2013-10-10T19:01:27.343 回答
17

我从不同的角度解决了这个问题。

我想如果服务器发送带有Content-Type: text/html标头的 JSON 数据。这并不意味着服务器人员打算向您发送一些 html 但意外更改为 JSON。这确实意味着服务器人员只是不关心Content-Type标头是什么。因此,如果服务器人员不关心客户端,那么您最好也忽略Content-Type标头。忽略Content-Type标题签入AFNetworking

manager.responseSerializer.acceptableContentTypes = nil;

这样,AFJSONResponseSerializer(默认)将序列化 JSON 数据而不检查Content-Type响应头。

于 2015-01-27T03:44:41.377 回答
7

启用接收“文本/纯文本”内容类型的简单方法:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

同样,如果您希望启用“text/html”内容类型:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
于 2014-06-03T18:20:23.920 回答
5

我按照@Andrie的回答尝试了下面的行,但没有奏效,

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

所以在狩猎更多之后,我确实设法让它成功工作。

这是我的代码片段。

AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];

    NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
    [jsonAcceptableContentTypes addObject:@"text/plain"];
    jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
    operation.responseSerializer = jsonResponseSerializer;

希望这会帮助那里的人。

于 2015-07-09T11:58:00.587 回答
3

这是我发现的唯一工作

-(void) testHTTPS {
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager GET:[NSString stringWithFormat:@"%@", HOST] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@", string);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}
于 2014-08-22T19:36:20.977 回答
3

如果有人在使用,AFHTTPSessionManager那么可以这样做来解决问题,

AFHTTPSessionManager在我正在做的地方分类,

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:self.responseSerializer.acceptableContentTypes];
[contentTypes addObject:@"text/html"];
self.responseSerializer.acceptableContentTypes = contentTypes;
于 2014-09-30T11:42:28.273 回答
2

就我而言,我无法控制服务器设置,但我知道“Content-Type”需要“application/json”。我在 iOS 客户端做了这个:

manager.requestSerializer = [AFJSONRequestSerializer serializer];

请参阅 AFNetworking 版本 2 内容类型错误

于 2014-09-02T01:26:41.793 回答
1

只需添加这一行:

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
于 2015-06-19T08:08:00.673 回答
0

我在使用 Swift 代码库中的 AFNetworking 时遇到了某种类似的问题,所以我只是把它留在这里,因为有人像我一样不幸不得不在这样的设置中工作。如果你是,我觉得你哥们,保持坚强!

acceptableContentTypes尽管我实际上设置了Set包含相关内容类型值的a ,但由于“不可接受的内容类型”,操作失败了。

对我来说,解决方案是调整 Swift 代码,使其对 Objective-C 更友好,我猜

serializer.acceptableContentTypes = NSSet(array: ["application/xml", "text/xml", "text/plain"]) as Set<NSObject>
于 2016-05-16T01:56:38.103 回答
0

一个好问题总是有多个答案,以减少并帮助您选择正确的答案,我在这里也添加自己的答案。我已经对其进行了测试,并且效果很好。

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.yourdomain.com/appname/data/ws/index.php/user/login/"]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

[manager POST:@"POST" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", json);
    //Now convert json string to dictionary.
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error.localizedDescription);
}];
于 2016-07-13T06:02:22.390 回答
-1
 UIImage *image = [UIImage imageNamed:@"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);


NSString *queryStringss = [NSString stringWithFormat:@"http://119.9.77.121/lets_chat/index.php/webservices/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

[MBProgressHUD showHUDAddedTo:self.view animated:YES];


[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {


     [formData appendPartWithFileData:imageData name:@"fileName" fileName:@"decline_clicked.png" mimeType:@"image/jpeg"];



 }
      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {



    NSDictionary *dict = [responseObject objectForKey:@"Result"];

    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];


 }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
     NSLog(@"Error: %@ ***** %@", operation.responseString, error);
 }];
于 2014-08-18T06:47:44.470 回答