【问题标题】:AFNetworking replacing - Replace for AFHTTPClientAFNetworking 替换 - 替换为 AFHTTPClient
【发布时间】:2014-07-09 06:57:34
【问题描述】:

我正在使用下面的代码通过服务执行我的 web 服务调用。我使用 AFHTTPClient 的 2.0 以下版本的 AFNetworking。现在我迁移到了最新版本的 AFNetworking。我在最新版本中找不到 AFHTTPClient 类。我应该用当前代码替换什么以便它再次工作。请提供任何帮助

@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];
}

【问题讨论】:

    标签: ios objective-c xcode cocoa-touch afnetworking


    【解决方案1】:

    你可以使用NSURLSession 来处理很多AFHTTPClient 的东西。 但是要实现所有功能,只需像现在一样编写一个基于 NSObject 的类。

    NSURLSession 有一个非常好的 API 和与之相结合的强大功能。

    【讨论】:

      【解决方案2】:

      AFHTTPRequestOperationManager 是子类的替代类,而不是 AFHTTPClient。它不一样,但它可能是你要找的。​​p>

      我建议您阅读 Mattt Thompson 的博客 NSHipster。他是 AFNetworking 的作者,并在不久前报道了这些变化http://nshipster.com/afnetworking-2/。还有一个 AFNetworking 2.0 迁移指南 https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide 对您很有用。

      【讨论】:

        【解决方案3】:

        最后我能够用 AFHTTPRequestOperationManager 替换 AFHttpClient 进行以下更改

        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小时前提出的解决方案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-13
        • 1970-01-01
        • 1970-01-01
        • 2015-01-14
        • 2020-12-30
        • 2011-03-15
        • 1970-01-01
        相关资源
        最近更新 更多