• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 AFJSONRequestOperation 中接受无效的 JSON?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:23:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在修复现有项目中的错误。问题是 AFHTTPClient 期望一个有效的 JSON 响应,但服务器返回一些像 ""\"Operation complete\""之类的乱码(所有引号和括号都在返回中)。这导致操作失败,并命中失败 block ,因为它无法解析响应。尽管如此,服务器正在返回状态码 200,并且很高兴操作完成。

我有一个像这样扩展 AFHTTPClient 的类(从我的 .h 文件中提取)

@interface AuthClient : AFHTTPClient

  //blah blah blah

@end

在我的实现文件中,类的初始化如下:

- (id)initWithBaseURLNSURL *)url{
    if (self = [super initWithBaseURL:url]) {        
        self.parameterEncoding = AFFormURLParameterEncoding;
        self.stringEncoding = NSASCIIStringEncoding;

        [self setDefaultHeader"Accept" value"application/json"];
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    }

    return self;
}

我正在调用的电话是在上面提到的类(class)中完成的,并且发生的情况如下:

- (void)destroyTokenNSString *)token onCompletionvoid (^)(BOOL success))completion onFailurevoid (^)(NSError *error))failure{

    [self postPath"TheServerURL" parameters{@"token": token} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error;
        //Do some stuff
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //Don't want to be here.
    }];
}

在失败 block 中,错误对象的 _userInfo 部分将返回错误:

[0] (null) @"NSDebugDescription": @"JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。"

错误码是3480。

通过谷歌搜索,我发现我需要设置类似 NSJSONReadingAllowFragments 的东西,但我不确定当前设置的方式/位置。有人有什么想法吗?



Best Answer-推荐答案


听起来响应根本不是 JSON。那么,为什么不直接接受 HTTP 响应本身,而不是尝试像 JSON 一样处理它呢?

如果您正在提交 JSON 格式的请求,但只想返回文本响应,则使用 AFNetworking 2.0 您可以执行以下操作:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:urlString parameters{@"token":token} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"responseString: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

或者,在 1.x 中:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
client.parameterEncoding = AFJSONParameterEncoding;
NSMutableURLRequest *request = [client requestWithMethod"OST" path:path parameters{@"token" : token}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"response: %@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[client enqueueHTTPRequestOperationperation];

关于ios - 在 AFJSONRequestOperation 中接受无效的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19965151/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap