"请求失败:不可接受的内容类型:text / html"使用AFNetworking 2.0

时间:2014-11-05 11:36:24

标签: ios asynchronous afnetworking-2

我正在尝试下面的代码,但它给出了错误:

{    
 NSURL *url = [NSURL URLWithString:@"http://ielmo.xtreemhost.com/array.php"];    
 NSURLRequest *urlRequest =[[NSURLRequest alloc]initWithURL:url];    
 AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];    
 requestOperation.responseSerializer = [AFImageResponseSerializer serializer];    
 [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        _imV.image = responseObject;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image error: %@", error);
    }];
    [requestOperation start];
}

请在"Request failed: unacceptable content-type: text/html"错误时帮助我。

3 个答案:

答案 0 :(得分:6)

请尝试以下代码:

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

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

答案 1 :(得分:4)

尝试

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];

这是为了确保您序列化请求&响应HTTP而不是JSON。

答案 2 :(得分:2)

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

这对我有用。看起来这与 AFNetworking 2.0 不同。显然,在 3.0 中,您需要明确提供requestSerializerresponseSerializer