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


当前回答

只要加上这一行:

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);
 }];

我从另一个角度来解决这个问题。

我认为如果服务器发送带有Content-Type: text/html头的JSON数据。这并不意味着服务器人员打算向你发送一些html,但不小心将其更改为JSON。这意味着服务器并不关心Content-Type报头是什么。所以如果服务器端不关心客户端,你最好也忽略Content-Type头。忽略AFNetworking中的Content-Type头检查

manager.responseSerializer.acceptableContentTypes = nil;

通过这种方式,AFJSONResponseSerializer(默认的)将序列化JSON数据,而不检查响应头中的Content-Type。

只要加上这一行:

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

在服务器端,我添加了:

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

到我的。php代码,这也解决了这个问题。

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

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

类似地,如果你希望启用"text/html"内容类型:

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