AFNetworking(AFJSONRequestOperation)转换为AFHTTPClient [英] AFNetworking (AFJSONRequestOperation) convert to AFHTTPClient
本文介绍了AFNetworking(AFJSONRequestOperation)转换为AFHTTPClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,但我需要对它进行更多控制,尤其需要在0.9中开始使用Reachability代码。
I have the following code that works well but I need a little more control over it and especially need to start using the Reachability code in 0.9.
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。
I am struggling to work out how I can convert this same code across to using an AFHTTPClient so that I can take advantage of the "setReachabilityStatusChangeBlock".
推荐答案
只需用单例创建一个AFHTTPClient的子类
Just create a subclass of AFHTTPClient with a singleton
+ (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方法
And then call the getPath method
[[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]);
}];
这篇关于AFNetworking(AFJSONRequestOperation)转换为AFHTTPClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文