(可可错误3840.)"(字符0周围的值无效.)AFNetworking

Jon*_*han 28 cocoa-touch json objective-c ios afnetworking-2

使用GET方法从服务器检索文件时,我一直收到以下错误:

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x16e81ed0 {NSDebugDescription=Invalid value around character 0.}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多不同的东西,我相信它可能与我想要获得的文件上的JSON格式有关.

这是我一直在使用的代码:

_username = @"JonDoe";
NSDictionary *parameters = @{ @"username" : _username};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

[manager GET:@"http://.........."
  parameters:parameters
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"JSON: %@", responseObject);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
     }];
Run Code Online (Sandbox Code Playgroud)

我的POST方法工作正常.我似乎无法用GET解决这个问题.有任何想法吗?谢谢.

Bra*_*red 39

从评论中的讨论来看,您的GET请求似乎是成功的(响应代码200),但响应正文是您使用请求时无效的JSON(也不是JSON片段)AFJSONResponseSerializer.基本AFHTTPResponseSerializer可用于非JSON的响应.