- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直试图在将请求分派(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/
我不知道我做了什么来破坏我的 QtCreator,但是“突然”我所有的编译器问题(警告/错误)不再显示在问题面板中,而是只显示在“编译输出”面板中(我在那里不能使用它们通过单击跳转到错误代码)。 在“
我正在制作游戏作为自学。 我的目标是在鼠标指针后面有一条轨迹。(这有效)但是,当我在 chrome 上打开并运行它时。在达到一定的高度和宽度后,轨迹保留在 Canvas 上而不是被擦掉。谁能帮我解决这
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我希望通过在我的HTML文件中适当地格式化我的网站上的文本来增强它的外观。具体地说,我希望确保我的HTML文件中的代码在网站上显示得很好。此外,我正在寻求帮助实现我的网站导航栏。,我希望和其他内容显示
我一直在尝试将背景图像添加到 html 文档中元素的边框: 你可以在这里看到:HTML: Something CSS: a { border-width:10px; border-co
我即将开始一个连接两个程序的项目,一个用c#,一个用c++。我已经有一个可以工作的 c# 程序,它能够与自身的其他版本对话。在开始使用c++版本之前,我想到了一些问题: 1) 我正在使用 protob
我正在尝试线程优先级的影响,当 run 方法中的 println 保留在注释中时,两个线程同时结束,我不理解这种行为,你能解释一下吗?谢谢。 主类 public class Main { public
jQuery 1.4: $('#menu li:last:not(.ignore)').addClass('last'); HTML: item 1 ite
当我创建或查看问题时,我想从Mantis问题跟踪系统中删除选择配置文件及其所有随附字段,因为这些问题与我要创建的任何问题都不相关。 其中包括配置文件部分随附的“平台”,“操作系统”和“操作系统版本”字
我不在Master分支上工作。我正在开发另一种newFeature,该功能也发布在github上。 我知道在处理Master分支时如何解决问题:Closes #XXX。 但是,这仅在我在Master分
我需要在一些UICollectionViewCell中画一个圆圈。具有不同颜色边框和背景颜色的圆圈。我的代码。 UICollectionViewController - (UICollectionVi
我们将Trac用于我正在从事的开源项目。我想将所有公开票导出到Github的Issues。我已经找到了一些可能的小型脚本,但是我想知道是否有人这样做并提出了建议。 理想情况下,它将trac的描述语法转
我对 iOS 编程还比较陌生,但在其他语言(主要是 C# 和 Java)方面拥有丰富的编程经验。 我正在尝试为 iPad 设备的某些产品制作一个类似图片库的东西,其中包含指定的类别。用户界面非常简单,
我已经在中间人中激活了 I18n,如下所示: activate :i18n, mount_at_root: :de 现在我想自动从 / 重定向到 /de。这可能吗? 另外,我想知道为什么中间人使用 p
我已经成功编写了一个程序,可以从 Excel 文件中读取数据并将它们存储在 mysql 的表中。我的程序读取每个文件的第一行,创建表的字段,并将其余数据存储为每列中的值。正是因为这种情况以编程方式发生
这是我的代码。 function m1() { return new Promise(function(resolve, reject) { var files = $('#aws-upl
谁能告诉我这是怎么回事? #include #include #include #include #include class writeManager { std::vector
compile 'com.github.RogaLabs:social-login:1.2.1' compile 'com.github.mukeshsolanki:social-login-help
我有一个巨大的 csv 文件,其中包含 149 列 和 25K+ 行,可以在 MySQL 中上传该文件> 我正在使用MySQL LOAD DATA 查询 表我的查询是: LOAD DATA local
我是一名优秀的程序员,十分优秀!