您好我正在创建一个AFNetworking 3.0 Singleton以在整个应用中使用。
这是我的单身人士
我的ApiClient.h
@interface ApiClient : AFHTTPSessionManager
+ (ApiClient *)sharedInstance;
我的ApiClient.m
+ (ApiClient *)sharedInstance {
static ApiClient *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];
});
return sharedInstance;
}
- (id)initWithBaseURL:(NSURL *)url
{
if ((self = [super initWithBaseURL:url])) {
AFJSONRequestSerializer *serializer = [AFJSONRequestSerializer serializer];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[serializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
}
return self;
}
我的设计有问题。每当我打电话给[Api Client] shareIntance
我需要包括
[ApiClient sharedInstance].responseSerializer.acceptableContentTypes = [[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
例如
- (void)getSingpostContentsOnSuccess:(ApiClientSuccess)success onFailure:(ApiClientFailure)failure {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"singpost-contents.php"
relativeToURL:[NSURL URLWithString:CMS_BASE_URL]]];
[ApiClient sharedInstance].responseSerializer.acceptableContentTypes = [[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSURLSessionDataTask *dataTask = [ApiClient.sharedInstance dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
failure(error);
// [self reportAPIIssueURL:[request.URL absoluteString] payload:nil message:[error description]];
} else {
NSDictionary *jsonDict = (NSDictionary *) responseObject;
success(jsonDict);
}
}];
[dataTask resume];
}
如果不存在,则会出现不可接受的内容类型错误。有多余的?
可以移动吗?[ApiClient sharedInstance].responseSerializer.acceptableContentTypes = [[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
启动shareIntance
我尝试使用
减少上面的代码[[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
哪个也不起作用
任何帮助或建议都非常感谢!谢谢!