AFNetworking - 发布http数据后卡住了

时间:2018-02-05 06:10:11

标签: ios objective-c afnetworking

我从2013年开始继承了一些Objective-C代码:太老了以至于它使用了AFNetworking 1.0!

@implementation AFClaimNotificationAPIClient

+ (AFClaimNotificationAPIClient *)sharedClient {
    static AFClaimNotificationAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFClaimNotificationAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kClaimNotificationURL]];
    });

    return _sharedClient;
}

- (void) submit:(ClaimNotification *) claimNotification
      success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {

    NSMutableURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:kClaimNotificationURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        [self populateFormDataForJson:formData andClaimNotification:claimNotification];
        [self populateFormDataWithAttachemnts:formData andClaimNotification:claimNotification];

    }];

    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
    DDLogVerbose(@"%@", request.HTTPBody);
    [operation setCompletionBlockWithSuccess:success failure:failure];

    [operation start];  
}

在此类的相应头文件中,定义了AFClaimNotificationAPIClient:

@interface AFClaimNotificationAPIClient : AFHTTPClient

和AFHTTPClient不再存在。它被删除在AFNetworking 2.0中,该代码写完后不久就出现了。

经过多次论坛搜索后,我实际上通过升级到AFNetworking 2.0并将AFClaimNotificationAPIClient重新定义为AFHTTPSessionManager来实现部分工作:

@interface AFClaimNotificationAPIClient : AFHTTPSessionManager

我的提交按钮代码现在看起来像这样:

- (void) submit:(ClaimNotification *) claimNotification
      success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {

    NSURLSessionDataTask *request = [self POST:kClaimNotificationURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [self populateFormDataForJson:formData andClaimNotification:claimNotification];
        [self populateFormDataWithAttachemnts:formData andClaimNotification:claimNotification];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        DDLogVerbose(@"Post success");
        // handle success
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        DDLogVerbose(@"Post error");

    }];
//    [operation start];
    [request resume];  // [request start] doesn't work  
}

我选择了AFHTTPSessionManager作为我的类的新类型,因为它是我能找到的唯一包含constructBodyWithBlock定义的类型,并且我试图使代码尽可能接近原始代码。

令人惊讶的是,我重写的代码实际上将数据发布到服务器并获得回复。但是,应用程序挂起,因为调用代码(此处未显示)未收到成功(或失败)消息。我可以看到我已从原始代码中删除了整个步骤 - 设置operation变量,然后触发其setCompletionBlockWithSuccess方法。

1 个答案:

答案 0 :(得分:1)

您需要根据传递responseObject的响应执行块或使用相应的操作

执行错误

试试这个

- (void) submit:(ClaimNotification *) claimNotification
      success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
      failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {

    NSURLSessionDataTask *request = [self POST:kClaimNotificationURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [self populateFormDataForJson:formData andClaimNotification:claimNotification];
        [self populateFormDataWithAttachemnts:formData andClaimNotification:claimNotification];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        DDLogVerbose(@"Post success");
        // handle success
        success(nil,responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        DDLogVerbose(@"Post error");
        failure(nil, error);
    }];
//    [operation start];
    [request resume];  // [request start] doesn't work  
}

您也可以使用AFHTTPRequestOperation,这将返回回调中所需的两个参数