I am basically building my app like the AFNetworking example that you can download from the AFNetworking 2.0 GitHub. However, in the past with AFNetworking 1.0 when I made a Client network extension class, I had to specify the initWithBaseURL that I can set acceptable content type.

In 2.0, it seems in the example does not have this instance method anymore. So, when I am using a web service, I am getting error [1] (null) @"NSLocalizedDescription" : @"Request failed: unacceptable content-type: text/html"

I tried the method of not using a AFClient class, and it works, but I want to set it up like the example, so I can have better code reuse.

Where can I set the contentType? Also, in the example for 2.0, they have a block call + (NSURLSessionDataTask *)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block { Before the return type would be void. What's the purpose of returning a NSURLSessionDataTask?

有帮助吗?

解决方案

the content-type of your response is text/html.

To get it, set the response Serializer of your AFHTTPSessionManager like that:

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

or you can try

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

其他提示

Figured it out.

I was setting the responseSerializer before the allocation... -_-. You need to put it int he sharedClient class method.

 + (instancetype)sharedClient {
static AFMobileClient *_sharedClient = nil;
static dispatch_once_t onceToken;


//_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
dispatch_once(&onceToken, ^{

    _sharedClient = [[AFMobileClient alloc] initWithBaseURL:[NSURL URLWithString:BaseURLString]];
    //_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    _sharedClient.responseSerializer = [AFHTTPResponseSerializer serializer];
});

return _sharedClient;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top