gpt4 book ai didi

ios - 设置 AFNetworking 3.x 单例请求和响应序列化

转载 作者:行者123 更新时间:2023-11-29 00:45:43 24 4
gpt4 key购买 nike

您好,我正在使用 AFNetworking 3.x。我为网络 api 构建了一个单例

+ (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;
}

我想在使用单例时设置请求和响应序列化。我们怎样才能做到这一点

我写这些代码总是出错。非常感谢任何帮助。谢谢

[[[ApiClient sharedInstance]requestSerializer = [AFHTTPRequestSerializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil]];

我的回复序列化

ApiClient sharedInstance].responseSerializer.acceptableContentTypes = [[ApiClient sharedInstance].responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

enter image description here

最佳答案

您需要将调用 [AFHTTPRequestSerializer requestWithMethod..] 更改为

[[AFHTTPRequestSerializer serializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil];

AFHTTPRequestSerializer 没有类方法 requestWithMethod 它需要先创建一个实例,然后调用 requestWithMethod

您的代码将如下所示:

[[ApiClient sharedInstance] requestSerializer] = [[AFHTTPRequestSerializer serializer] requestWithMethod:POST_METHOD URLString:urlString parameters:xml error:nil];

关于ios - 设置 AFNetworking 3.x 单例请求和响应序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com