【问题标题】:How to determine reason for failure with AFNetworking AFJSONRequestOperation如何使用 AFNetworking AFJSONRequestOperation 确定失败的原因
【发布时间】:2013-09-18 16:28:29
【问题描述】:

我继承了一个项目,正在实施 AFNetworking 并阅读文档,这听起来很棒,而且比当前代码简单得多。我有一个带有 json 数据的 url,所以我正在执行以下操作,但是得到了失败块并且不知道为什么。我确定它在那里,但是我如何深入研究 AF 并记录响应以确定失败原因。我知道 url 有效,但也许它点击了 url 但解析有问题?

NSString *listURL = [NSString stringWithFormat:GET_LIST,BASE_URL];
NSURL *url = [NSURL URLWithString:briefListURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                                                            self.list = [NSArray arrayWithArray:(NSArray *)JSON];                                                                                                    
                                                                                        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                            [self listOperationDidFail];
                                                                                        }];

【问题讨论】:

  • failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Error: %@", error); }
  • 哇非常感谢您的快速回复!这很容易,我得到了这个:错误:错误域=AFNetworkingErrorDomain Code=-1016“预期的内容类型{(“text/json”,“application/json”,“text/javascript”)},得到文本/纯文本“用户信息=0x8454e80 {NSLocalizedRecoverySuggestion=[{"newsletters":.....那么内容的转储我不是网络专家,但我内部的文档说这个 url 吐出 json,我猜它不是? afnetworking 可以解决这个问题吗?

标签: iphone ios networking afnetworking


【解决方案1】:

感谢@Larme 这成功了:

failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Error: %@", error); }

另外,用 text/plain 回答我自己的问题。只需在设置操作之前添加:

  [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 2020-08-28
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2016-05-16
    相关资源
    最近更新 更多