AFHTetworking与AFHTTPClient和AFJSONRequestOperation // MIME类型问题

时间:2012-11-01 18:06:41

标签: ios afnetworking

我一直试图在将请求分派给需要OAuth身份验证的基于REST的服务的特定实例中控制AFHTTPClient。使用GTMOAuth创建OAuth身份验证没有问题。

我还可以使用手工拼接的NSMutableURLRequest以及AFJSONRequestOperation和NSURLConnection成功地对参数进行编组以调度请求并获得格式良好的JSON响应。后两种机制是我的理智检查,我正确地接触了服务。

我使用

收到回复
[AFHTTPClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)] 

但无论如何 - 它被解释为text / plain。返回的对象的类是__NCFData。

没有bueno。

这段代码不想返回任何类型的字典的响应。

- (IBAction) testFlickr {
// marshall parameters
NSString *urlStr = @"http://api.flickr.com/";
NSURL *url = [NSURL URLWithString:urlStr];

AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setParameterEncoding:AFJSONParameterEncoding];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:@"json", @"format", @"66854529@N00", @"user_id", @"1", @"jsoncallback", nil];

NSString *path = [[NSString alloc]initWithFormat:@"services/rest/?method=flickr.people.getPhotos"];

NSMutableURLRequest *af_request = [client requestWithMethod:@"GET" path:path parameters:params];

// flickrAuth instance variable is an instance of GTMOAuthAuthentication
[self.flickrAuth authorizeRequest:af_request];

[client setAuthorizationHeaderWithToken:[self.flickrAuth accessToken]];
[client setDefaultHeader:@"Accept" value:@"application/json"];

[client requestWithMethod:@"GET" path:path parameters:params];

LOG_FLICKR_VERBOSE(0, @"Can Authorize? %@", ([self.flickrAuth canAuthorize] ? @"YES":@"NO"));
LOG_FLICKR_VERBOSE(0, @"%@", client);


// first way of trying..
AFHTTPRequestOperation *af_operation = [client HTTPRequestOperationWithRequest:af_request success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *str = [[NSString alloc] initWithData:responseObject
                                          encoding:NSUTF8StringEncoding];
    LOG_FLICKR_VERBOSE(0, @"Weird af_operation semantics, but.. %@", str);
    LOG_FLICKR_VERBOSE(0, @"Weird af_operation semantics returns %@", [responseObject class]);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //
    LOG_FLICKR_VERBOSE(0, @"Weird af_operation semantics, error.. %@", error);

}];

[af_operation start];
}

这个请求没问题。响应数据本身就是我所期望的,但它不是任何一种字典类。

我宁愿继续使用AFHTTPClient的方法(例如,[AFJSONRequestOperation JSONRequestOperationWithRequest]),所以我可以使用AFHTTPClient的Reachability方法等等。

奇怪(至少对我来说)如果我这样做的请求:

NSMutableURLRequest *aj_request = [client requestWithMethod:@"GET" path:path parameters:params];
[self.flickrAuth authorizeRequest:aj_request];

AFJSONRequestOperation *aj_operation = 
[AFJSONRequestOperation JSONRequestOperationWithRequest:af_request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    LOG_FLICKR_VERBOSE(0, @"AFJSONRequestOperation %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    LOG_FLICKR_VERBOSE(0, @"AFJSONREquestOperation Error %@", error);
}];

[aj_operation start];

它以“401”失败,因为它期望响应头中的application / json而不是认为它是收到的text / plain

但是,如果我这样做的请求:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[[NSURL alloc]initWithString:@"http://api.flickr.com/services/rest/?method=flickr.people.getPhotos&format=json&user_id=66854529@N00&nojsoncallback=1"]];
[self.flickrAuth authorizeRequest:request];

AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                    LOG_FLICKR_VERBOSE(0, @"Success Flickr  =========\n%@ %@", JSON, [JSON valueForKeyPath:@"photos.total"]);
                                                    /////handler(JSON, nil);
                                                } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                    LOG_FLICKR(0, @"URL Was %@", url);
                                                    LOG_FLICKR(0, @"Failed Flickr  ==========\n%@ %@", error, JSON);
                                                    /////handler(nil, error);
                                                }];
[operation start];

它工作正常,包括漂亮的JSON,字典形成的数据。

在第一个例子中,我使用AFHTTPClient来生成NSMutableURLRequest。在第二个例子中,我正在创建自己的NSMutableURLRequest。在这两种情况下,我都使用AFJSONRequestOperation来发送请求,留下问题的唯一罪魁祸首(除了我自己..)AFHTTPClient。

在我可以开始工作的第一个例子中,它没有返回JSON-y数据。

在第二个示例中,AFHTTPClient似乎创建了一个公然失败的NSMutableURLRequest - 但是(AFAICT)当使用[NSMutableURLRequest requestWithURL]“手动”创建该URL时,相同的URL成功。

我想知道 - 使用AFHTTPClient时我错过了什么?

帮助?

2 个答案:

答案 0 :(得分:2)

在您的第一个代码示例中,看起来您正在执行NSMutableURLRequest *af_request = [client requestWithMethod:@"GET" path:path parameters:params];,然后设置默认标头。默认标头仅应用于指定后创建的请求。也许这就是事情发生的原因。

此外,401错误可能会抱怨其内容类型,但401是错误状态代码,这意味着您未经身份验证。

答案 1 :(得分:0)

我最终删除了所有标头参数以隔离问题,但它没有任何区别。仔细检查反应给了我一个线索。虽然Flickr确实返回“JSON”但它不是无尘的,看起来并且需要对其中一个参数进行调整。我一直在发送jsoncallback = 1但它应该是nojsoncallback = 1。一旦我修复了该参数,AFJSONRequestOperation就会正确处理响应并解析JSON。

我的最终代码看起来像这样(对于其他代码,n.b。nojsoncallback = 1参数)

- (IBAction)testFlickrAFJSON:(id)sender
{
// marshall parameters
NSString *urlStr = @"http://api.flickr.com/";
NSURL *url = [NSURL URLWithString:urlStr];
//NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:@"json", @"format", @"66854529@N00", @"user_id", nil];
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:@"json", @"format", @"66854529@N00", @"user_id", @"1", @"nojsoncallback", nil];
NSString *path = [[NSString alloc]initWithFormat:@"services/rest/?method=flickr.people.getPhotos"];

AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];

NSMutableURLRequest *af_request = [client requestWithMethod:@"GET" path:path parameters:params];

[self.flickrAuth authorizeRequest:af_request];

LOG_FLICKR_VERBOSE(0, @"Can Authorize? %@", ([self.flickrAuth canAuthorize] ? @"YES":@"NO"));
AFJSONRequestOperation *af_operation_2 = [AFJSONRequestOperation JSONRequestOperationWithRequest:af_request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        LOG_FLICKR_VERBOSE(0, @"AFJSONRequestOperation Alt %@", JSON);
        LOG_FLICKR_VERBOSE(0,@"AFJSONRequestOperation Alt response MIMEType %@",[response MIMEType]);

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        LOG_FLICKR_VERBOSE(0, @"AFJSONREquestOperation Alt Error %@", error);
        NSHTTPURLResponse *resp = [[error userInfo] valueForKey:AFNetworkingOperationFailingURLResponseErrorKey];
        LOG_FLICKR_VERBOSE(0,@"AFJSONRequestOperation Alt Error response MIMEType %@",[resp MIMEType]);

    }];

    [af_operation_2 start];

}