【问题标题】:Cocoa Error 3840 on AFNetworkingAFNetworking 上的 Cocoa 错误 3840
【发布时间】:2013-10-19 03:10:52
【问题描述】:

我正在使用 AFNetworking 2.0 和 OHHTTPStubs 编写单元测试用例,但测试总是失败并出现以下错误:

响应错误:操作无法完成。 (可可错误 3840。)。

非常感谢!

下面是简单的测试Json(User.json): { "userId": "abc", “电子邮件”:“ab@pant.com”, “用户名”:“用户名” }

以及测试用例代码:

- (void)testGet
{
    [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
        return YES; // Stub ALL requests without any condition
    } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
        return [OHHTTPStubsResponse responseWithFileAtPath:OHPathForFileInBundle(@"User.json",nil)
            statusCode:200 headers:@{@"Content-Type":@"application/json"}];
    }];

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    AFHTTPSessionManager* requestManager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"http://test.com"]];
    AFJSONResponseSerializer* responseSerializer = [[[AFJSONResponseSerializer alloc]init] autorelease];
    responseSerializer.readingOptions = NSJSONReadingAllowFragments;
    responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 2)];
    requestManager.responseSerializer = responseSerializer;

    AFJSONRequestSerializer* requestSerializer = [[AFJSONRequestSerializer alloc] init];
    requestSerializer.writingOptions = NSJSONWritingPrettyPrinted;
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    requestManager.requestSerializer = requestSerializer;

    _done = NO;
    [requestManager GET:@"stream" parameters:nil success:^(NSURLSessionDataTask * task, id JSON)
    {
        NSLog(@"Response data:%@", JSON);
        XCTAssert(JSON != nil, @"null response");
        dispatch_semaphore_signal(semaphore);

    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        NSLog(@"Response error:%@", [error localizedDescription]);
        XCTFail(@"fail to get response");
        dispatch_semaphore_signal(semaphore);
    }];

    while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                 beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    }
}

【问题讨论】:

  • 你上面引用的 JSON 是有效的 JSON,但是 Cocoa 错误 3840 表示输入的 JSON 无效。 User.json 中的结构无效,或者 JSON 规范不支持您的文件编码。
  • 如果您记录完整的 NSError 而不仅仅是本地化描述,您会发现它会告诉您发生错误的 JSON 流的偏移量,这通常是由于非 UTF-8 字符造成的。跨度>

标签: ios afnetworking ohhttpstubs


【解决方案1】:

尝试改用 AFHTTPResponseSerializer。所以基本上,替换这段代码:

    AFJSONResponseSerializer* responseSerializer = [[[AFJSONResponseSerializer alloc]init] autorelease];
    responseSerializer.readingOptions = NSJSONReadingAllowFragments;
    responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 2)];
    requestManager.responseSerializer = responseSerializer;

到这个:

    requestManager.responseSerializer = [AFHTTPResponseSerializer serializer];

因为您使用的是 @"text/html" 的可接受内容类型,我假设您在响应调用时不需要 JSON 序列化程序。所以这对你的情况应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2013-12-18
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多