我正在尝试实施AFNetworking代码以与Web API进行通信。我在代码中遇到以下错误:
APIClass没有可见的@interface声明选择器 registerHTTPOperationClass
APIClass没有可见的@interface声明选择器 setDefaultHeader:值
APIClass没有可见的@interface声明选择器 multiPartFormRequestWithMethod:路径:参数:constructingBodyWithblock
显然与新的AFNetworking 2.0迁移有关...但是我一直在查看所有迁移帖子和文档,并且找不到这些的替代品而不会抛出错误:
// add the location details of the web service we wrote
#define kAPIHost @"http://myurl"
#define kAPIPath @"mywebapi/"
@implementation APIClass
// this is the implementation of the singleton method
+(APIClass*)sharedInstance{
static APIClass *sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:kAPIHost]];
});
return sharedInstance;
}
-(APIClass*)init{
// call super init
self = [super init];
if (self != nil){
user = nil;
[self registerHTTPOperationClass:[AFHTTPRequestOperation class]];
// Accept HTTP header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
// call to the server
-(void)commandWithParams:(NSMutableDictionary*)params onCompletion:
(JSONResponseBlock)completionBlock
{
// prepare e POST request by creating an NSMutableURLRequest instance using the
// parameters we want to send via POST
NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
path:kAPIPath
parameters: params
constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
// attach file if needed
}];
// create an operation to handle the network communication in the background
// and intialize it with the POST request we just prepared
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:
apiRequest];
// now set the 2 blocks needed for success and failure
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id
responseObject)
{
// success! - if the call is successful then we just pass in the JSON response
NSLog(@"responseObject: %@", responseObject);
completionBlock(responseObject);
}
// if there is a failure in the network call then we call the failure block
// and contrcut a new dictinary to hold the message of the network error
failure:^(AFHTTPRequestOperation *operation, NSError * error) {
//failure!
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription]forKey:@"error"]);
}];
// at this point we can call the start method so that AFNetworking can do its
// magic in the background
[operation start];
}
@end
答案 0 :(得分:1)
您收到这些错误是因为您调用的方法不属于您要进行子类化的任何类别的方法。我假设你要继承AFHTTPSessionManager
,这是AFNetworking 2.0中推荐用于iOS 7的。{1}}。基于此...
对于前两个错误,我相信下面更新的行是使用AFHTTPSessionManager
的AFNetworking 2.0方式:
-(APIClass*)init{
// call super init
self = [super init];
if (self != nil){
user = nil;
self.requestSerializer = [AFJSONRequestSerializer serializer];
self.responseSerializer = [AFJSONResponseSerializer serializer];
}
}
对于第三个错误,方法multiPartFormRequestWithMethod:path:parameters:constructingBodyWithblock
应替换为:
[self POST:kAPIPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// attach file if needed
} success:^(NSURLSessionDataTask *task, id responseObject) {
// handle success
} failure:^(NSURLSessionDataTask *task, NSError *error) {
// handle failure
}];