我正在尝试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。


当前回答

我从Swift代码库中使用AFNetworking工作时遇到了类似的问题,所以我只是把这个留在远程情况下,有人像我一样不幸,必须在这样的设置中工作。如果你是,我觉得你,伙计,坚强点!

操作由于“不可接受的内容类型”而失败,尽管我实际上设置了一个包含有问题的内容类型值的Set acceptableContentTypes。

我的解决方案是调整Swift代码,使其更适合Objective-C,我想:

serializer.acceptableContentTypes = NSSet(array: ["application/xml", "text/xml", "text/plain"]) as Set<NSObject>

其他回答

在我的情况下,我没有控制服务器设置,但我知道它期待“应用程序/json”为“内容类型”。我在iOS客户端是这样做的:

manager.requestSerializer = [AFJSONRequestSerializer serializer];

指 AFNetworking版本2内容类型错误

只要加上这一行:

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
 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);
 }];

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

我子类afhttpessionmanager,我这样做,

NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:self.responseSerializer.acceptableContentTypes];
[contentTypes addObject:@"text/html"];
self.responseSerializer.acceptableContentTypes = contentTypes;

这意味着您的服务器正在发送“text/html”而不是已经支持的类型。 我的解决方案是添加“文本/html”到acceptableContentTypes设置在AFURLResponseSerialization类。只需搜索“acceptableContentTypes”,并手动添加@“text/html”到集合。

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