使用AFNetworking 2.0访问DailyMile API

所以我从一个https站点(取回auth令牌后)发出JSON请求,并像堆栈溢出AFNetworking十几个其他问题,我得到这个错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8dc2860 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

我用来提出这个请求的代码就像这样:

 AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer]; [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; //Set auth header... NSString *accessToken = [[[self.dailymileAuthentication oauthClient] accessToken] accessToken]; [requestSerializer setAuthorizationHeaderFieldWithToken:accessToken]; AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager]; [requestManager setRequestSerializer:requestSerializer]; [requestManager setResponseSerializer:[AFJSONResponseSerializer serializer]]; [requestManager GET:[profileURL description] parameters:nil success:^(AFHTTPRequestOperation *operation, id response) { NSLog(@"JSON: %@", response); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; 

url是: https : //api.dailymile.com/people/me.json

如果任何人有兴趣检查完整的代码库(注意:你不需要了解这个问题,这是可选的),我正在使用公共GitHub回购: https : //github.com/thepost/ Dailymile-IOS

我需要做什么来进行validation的JSON请求?

我不知道我是否正确使用AFNetworking。 说实话,AFNetworking 2还没有很多文档。

我不确定混淆是什么; 错误很明显:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8dc2860 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

DailyMile的JSON响应无效,因为它没有数组或对象作为其根值。

如果该值确实是JSON,并且应该被parsing为JSON,那么可以将responseSerializerreadingOptions设置为允许片段。

然而,我怀疑你的API端点并不实际返回JSON,而是一个带有错误的Content-Type头部的纯文本响应。