Ios AFHTTPClient的替换
我正在使用下面的代码对该服务执行Web服务调用。我使用了低于2.0的AFHTTPClient版本。现在我迁移到了AFNetworking的最新版本。我在最新版本中找不到AFHTTPClient类。我应该用当前代码替换什么,这样它才能再次工作。有什么帮助吗Ios AFHTTPClient的替换,ios,objective-c,xcode,cocoa-touch,afnetworking,Ios,Objective C,Xcode,Cocoa Touch,Afnetworking,我正在使用下面的代码对该服务执行Web服务调用。我使用了低于2.0的AFHTTPClient版本。现在我迁移到了AFNetworking的最新版本。我在最新版本中找不到AFHTTPClient类。我应该用当前代码替换什么,这样它才能再次工作。有什么帮助吗 @interface APIClient : AFHTTPClient + (APIClient*)client; - (void)commandWithMethod:(NSString *)method params:(NSMutable
@interface APIClient : AFHTTPClient
+ (APIClient*)client;
- (void)commandWithMethod:(NSString *)method params:(NSMutableDictionary*)params success:(APIClientSuccessCallback)successBlock failure:(APIClientFailureCallback)failureBlock;
@end
// Singleton method
+ (APIClient*)client {
static APIClient *client = nil;
static dispatch_once_t onceInst;
dispatch_once(&onceInst, ^{
client = [[self alloc] initWithBaseURL:[NSURL URLWithString:APIHost]];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:
@"application/json",
@"text/json",
@"text/javascript",
@"text/plain",
@"text/html",
@"application/x-www-form-urlencoded", nil]];
});
return client;
}
#pragma mark - Init
// Intialize the API class with the destination host name
- (APIClient*)init {
self = [super init]; // call super init
if (self != nil) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
#pragma mark - Core API Methods
// This function sends an API call to the server
- (void)commandWithMethod:(NSString *)method params:(NSMutableDictionary*)params success:(APIClientSuccessCallback)successBlock failure:(APIClientFailureCallback)failureBlock {
[MBMNetworkActivity pushNetworkActivity];
NSMutableURLRequest *apiRequest = [self requestWithMethod:@"POST" path:method parameters:params];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// success! :)
[MBMNetworkActivity popNetworkActivity];
successBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure! :(
[MBMNetworkActivity popNetworkActivity];
failureBlock(error);
}];
[operation start];
}
您可以将
NSURLSession
用于相当多的AFHTTPClient
内容。
但要实现所有功能,只需编写一个类似于您现在所做的基于NSObject的类
NSURLSession
有一个非常好的API和强大的功能。AFHTTPRequestOperationManager是子类而不是AFHTTPClient的替换类。这不一样,但可能是你想要的
我建议你读一下马特·汤普森的博客NSHipster。他是AFNetworking的作者,不久前报道了这些变化。还有一个对您有用的AFNetworking 2.0迁移指南。最后,我可以做以下更改,将AFHttpClient替换为AFHTTPRequestOperationManager
typedef void (^APIClientSuccessCallback) (id response);
typedef void (^APIClientFailureCallback) (id error);
@interface APIClient : AFHTTPRequestOperationManager
+ (APIClient*)client;
- (void)commandWithMethod:(NSString *)method params:(NSMutableDictionary*)params success:(APIClientSuccessCallback)successBlock failure:(APIClientFailureCallback)failureBlock;
@end
#import "APIClient.h"
@implementation APIClient
+ (APIClient*)client {
static APIClient *client = nil;
static dispatch_once_t onceInst;
dispatch_once(&onceInst, ^{
client = [[self alloc] initWithBaseURL:[NSURL URLWithString:APIHost]];
client.responseSerializer = [AFJSONResponseSerializer serializer];
[client.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
});
return client;
}
#pragma mark - Core API Methods
// This function sends an API call to the server
- (void)commandWithMethod:(NSString *)method params:(NSMutableDictionary*)params success:(APIClientSuccessCallback)successBlock failure:(APIClientFailureCallback)failureBlock {
[self POST:method parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"response --- %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error ----- %@",error);
}];
}
@end
这就是我23小时前提出的解决方案