gpt4 book ai didi

ios - AFNetworking with AFHTTPClient with AFJSONRequestOperation//MIME-Type Issues

转载 作者:行者123 更新时间:2023-11-28 22:46:08 24 4
gpt4 key购买 nike

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

我还可以使用手工拼凑的 NSMutableURLRequest 以及 AFJSONRequestOperation 和 NSURLConnection 成功编码参数以分派(dispatch)请求并获得格式正确的 JSON 响应。后两个机制是我的理智检查,以确保我正确接触了服务。

我使用

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

但无论如何 — 它都被解释为文本/纯文本。返回对象的类是 __NCFData。

没有布埃诺。

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

- (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”,因为它期望响应 header 中有 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 时我错过了什么?

帮忙吗?

最佳答案

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

此外,401 错误可能是在提示其内容类型,但 401 是一个错误状态代码,表示您未通过身份验证。

关于ios - AFNetworking with AFHTTPClient with AFJSONRequestOperation//MIME-Type Issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com