gpt4 book ai didi

iphone - AFNetworking (AFJSONRequestOperation) 转换为 AFHTTPClient

转载 作者:行者123 更新时间:2023-12-03 18:52:18 24 4
gpt4 key购买 nike

我有以下运行良好的代码,但我需要对其进行更多控制,尤其需要在 0.9 中开始使用 Reachability 代码。

NSString *urlString = [NSString stringWithFormat:@"http://example.com/API/api.php"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
_self.mainDictionary = [JSON valueForKeyPath:@"elements"];
[_self parseLiveData];
} failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON){
//NSLog(@"Failed: %@",[error localizedDescription]);
}];

if (operation !=nil && ([self.sharedQueue operationCount] == 0)) {
[self.sharedQueue addOperation:operation];
}

我正在努力弄清楚如何将相同的代码转换为使用 AFHTTPClient,以便我可以利用“setReachabilityStatusChangeBlock”。

最佳答案

只需使用单例创建 AFHTTPClient 的子类

+ (id)sharedHTTPClient
{
static dispatch_once_t pred = 0;
__strong static id __httpClient = nil;
dispatch_once(&pred, ^{
__httpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/API"]];
[__httpClient setParameterEncoding:AFJSONParameterEncoding];
[__httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
});
return __httpClient;
}

然后调用getPath方法

[[YourHTTPClient sharedHTTPClient]
getPath:@"api.php"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id JSON){
_self.mainDictionary = [JSON valueForKeyPath:@"elements"];
[_self parseLiveData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//NSLog(@"Failed: %@",[error localizedDescription]);
}];

关于iphone - AFNetworking (AFJSONRequestOperation) 转换为 AFHTTPClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9411364/

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