AFNetworking 2.0 实现代码不起作用

Posted

技术标签:

【中文标题】AFNetworking 2.0 实现代码不起作用【英文标题】:AFNetworking 2.0 implementation code not working 【发布时间】:2014-05-08 15:48:36 【问题描述】:

我正在尝试实现 AFNetworking 代码以与 Web API 进行通信。我在代码中收到以下错误:

APIClass 没有可见的@interface 声明选择器 注册HTTPOperationClass

APIClass 没有可见的@interface 声明选择器 设置默认标题:值

APIClass 没有可见的@interface 声明选择器 multiPartFormRequestWithMethod:path:parameters: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

【问题讨论】:

你是哪个 AFNetworking 类的子类? 我正在继承 AFHTTPSessionManager 【参考方案1】:

您收到这些错误是因为您调用的方法不是您子类化的任何类的方法。我假设您正在继承 AFHTTPSessionManager,这是在 AFNetworking 2.0 中推荐用于 ios 7 的。以此为基础...

对于您的前两个错误,我相信下面的更新行是 AFNetworking 2.0 使用 AFHTTPSessionManager 的方式:

-(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
];

【讨论】:

感谢您的回复。我不知道如何将我当前的 commandWithParams 转换为 POST:parameters:constructingBodyWithBlock:success:failure:. @Cyber​​netic - 好吧,我不确定你具体需要做什么,但我用可能的实现更新了我的答案。

以上是关于AFNetworking 2.0 实现代码不起作用的主要内容,如果未能解决你的问题,请参考以下文章

obj-c AFNetworking 2.0 POST 请求不起作用

AFNetworking 2.0 多部分/表单数据上传到 mySQL

使用AFnetworking以多部分格式上传图像在ios中不起作用

AFNetworking 3.0 后台上传不起作用

AFNetworking-2 waitUntilFinished 不起作用

AFNetworking POST 请求不起作用?