OGeek|极客世界-中国程序员成长平台

标题: ios - 在 AFJSONRequestOperation 中接受无效的 JSON? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:23
标题: ios - 在 AFJSONRequestOperation 中接受无效的 JSON?

我正在修复现有项目中的错误。问题是 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4