0

我的应用程序AFNetworking用于 twitter API 访问,我通过子类化创建了一个 twitter api 客户端AFHTTPClient

#import "AFHTTPClient.h"

@interface TwitterAPIClient : AFHTTPClient

+ (TwitterAPIClient *)sharedClient;

@end

#import "TwitterAPIClient.h"
#import "AFJSONRequestOperation.h"

static NSString * const kAFTwitterAPIBaseURLString = @"http://api.twitter.com/1/";

@implementation TwitterAPIClient

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

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end

如果我getPath's & postPath's在 上使用TwitterAPIClient,API 客户端会正确返回 JSON 响应,因为我将 a 注册AFJSONRequestOperation为操作类。

但是,有时,我需要创建自定义NSMutableURLRequest请求而不使用这些getPath's & postPath's AFHTTPClient功能。

当我使用这些请求时,从客户端返回的响应是标准的NSData,而不是NSDictionary我从AFJSONRequestOperation.

 NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/account/verify_credentials.json"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [self.auth authorizeRequest:request];

    AFHTTPRequestOperation* apiRequest = [[TwitterAPIClient sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, NSDictionary* responseObject) {
        [self createAccount];

        self.account.username = [responseObject objectForKey:@"screen_name"];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.delegate didProfileLoaded:self.account];
        });


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error!=nil) {
            NSString* errorMessage = nil;
            NSString* errorData = [error.userInfo objectForKey:NSLocalizedRecoverySuggestionErrorKey];

            if (errorData!=nil) {
                NSError* error;
                NSDictionary* json = [NSJSONSerialization JSONObjectWithData:[errorData dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
                if (json!=nil && error==nil) {
                    NSArray* errorMeta = [json objectForKey:@"errors"];
                    if (errorMeta!=nil) {
                        errorMessage = [[errorMeta objectAtIndex:0] objectForKey:@"message"];
                    }
                } else {
                    errorMessage = errorData;
                }
            }

            dispatch_async(dispatch_get_main_queue(), ^{
                [self.delegate didUpdateFailed:errorMessage];
            });
        }

    }];

    [[TwitterAPIClient sharedClient] enqueueHTTPRequestOperation:apiRequest];

有没有办法可以强制AFHTTPRequestOperation将它们创建为AFJSONRequestOperation对象?

4

1 回答 1

1

只需直接创建一个 AFJSONRequestOperation ,如下所示:

    AFJSONRequestOperation* apiRequest = [[AFJSONRequestOperation alloc] initWithRequest:request];
[apiRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

// ....

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

// ...

}];

更多示例在这里这里

于 2013-03-12T09:59:25.473 回答