AFHTetworking与AFHTTPClient和AFJSONRequestOperation // MIME类型问题 [英] AFNetworking with AFHTTPClient with AFJSONRequestOperation // MIME-Type Issues

查看:133
本文介绍了AFHTetworking与AFHTTPClient和AFJSONRequestOperation // MIME类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to get a grip on AFHTTPClient in the specific instance of dispatching a request to a REST-based service that requires OAuth authentication. I have no problem with creating the OAuth authentication using GTMOAuth.

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

I can also successfully marshall parameters to dispatch the request and obtain a well-formed JSON response using a hand-cobbled NSMutableURLRequest and both AFJSONRequestOperation and an NSURLConnection. Those latter two mechanics were my sanity check that I was touching the service correctly.

我收到回复

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

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

but no matter what — it's interpreted as text/plain. The returned object's class is __NCFData.

没有bueno。

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

This bit of code doesn't want to return a response that's a dictionary of any sort.

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

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

This request goes through okay. The response data itself is what I'd expect, but it is not any kind of dictionary class.

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

I'd rather keep to using methods of AFHTTPClient (as opposed to, for example, [AFJSONRequestOperation JSONRequestOperationWithRequest]) so I can use AFHTTPClient's Reachability methods and so forth.

奇怪(至少对我而言)如果我这样做请求:

Strangely (to me, at least) if I do the request like this:

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而是认为它是收到的文字/普通

It fails with a "401" because it was expecting application/json in the response header and instead thinks it's received text/plain

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

But, if I do the request like this:

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,字典形成的数据。

It works fine, including nice JSON, dictionary-formed data.

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

In the first instance, I'm using AFHTTPClient to produce the NSMutableURLRequest. In the second instance, I'm creating the NSMutableURLRequest on my own. In both cases I'm using AFJSONRequestOperation to dispatch the request leaving the only culprit for the problem to (besides myself..) AFHTTPClient.

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

In the first example that I can get to work, it's not returning JSON-y data.

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

In the second example AFHTTPClient seems to create an NSMutableURLRequest that blatantly fails — but (AFAICT) the same URL succeeds when that URL is created "by hand" using [NSMutableURLRequest requestWithURL].

我想知道 - 使用AFHTTPClient时我缺少什么?

I wonder — what am I missing when using AFHTTPClient?

帮助?

推荐答案

在你的第一个代码示例中,看起来你正在做 NSMutableURLRequest * af_request = [client requestWithMethod:@GETpath:path parameters:params]; 然后设置默认标题。默认标头仅应用于指定后创建的请求。也许这就是问题的原因。

In your first code example, it looks like you're doing NSMutableURLRequest *af_request = [client requestWithMethod:@"GET" path:path parameters:params]; and then setting default headers afterwards. Default headers only get applied to requests created after they were specified. Maybe that's where things are going amiss.

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

Also, that 401 error may be complaining about its content type, but 401 is an error status code, meaning that you're unauthenticated.

这篇关于AFHTetworking与AFHTTPClient和AFJSONRequestOperation // MIME类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆