通过AFNetworking 2.0访问DailyMile API [英] Accessing DailyMile API with AFNetworking 2.0
问题描述
因此,我正在从https站点发出JSON请求(检索了auth令牌后),并且像堆栈溢出时AFNetworking的其他十二个问题一样,我收到此错误:
So I'm making a JSON request from an https site (after retrieving the auth token) and like a dozen other problems with AFNetworking on stack overflow, I'm getting this error:
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.}
我用来发出此请求的代码如下:
The code I'm using to make this request is like such:
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仓库中工作(注意:您不需要 来理解问题,这只是可选的):
< a href = https://github.com/thepost/Dailymile-iOS rel = nofollow> https://github.com/thepost/Dailymile-iOS
I'm working out of a public GitHub repo if anyone is interested in checking out the full codebase (note: you don't need to to understand the problem, this is just optional): https://github.com/thepost/Dailymile-iOS
发出经过身份验证的JSON请求需要做什么?
What do I need to do to make an authenticated JSON request?
我不知道是否正确使用了AFNetworking。老实说,AFNetworking 2尚无大量文档。
I have no idea if I'm using AFNetworking correctly or not. To be honest, there isn't a lot of documentation out there for AFNetworking 2 yet.
推荐答案
我不确定混乱即将到来;错误非常清楚:
I'm not sure what the confusion is about; the error is quite clear:
错误Domain = NSCocoaErrorDomain代码= 3840操作无法完成。(可可错误3840。)(JSON文本不是以数组或对象开头,并且没有允许设置片段的选项。)UserInfo = 0x8dc2860 {NSDebugDescription = JSON文本不是以数组或对象开头并且没有允许设置片段的选项。}
DailyMile的JSON响应无效,因为它的根值没有数组或对象。
The JSON response from DailyMile is not valid, as it doesn't have an array or object as its root value.
如果该值确实是JSON,并且应解析为JSON,则可以设置 responseSerializer
readingOptions > 允许片段。
If the value is indeed JSON, and should be parsed as JSON, you can set the readingOptions
of the responseSerializer
to allow fragments.
但是,我怀疑您的API端点实际上不是返回JSON,而是纯文本响应,其中包含错误的 Content-Type
标头。
What I suspect, however, is that your API endpoint is not actually returning JSON, but a plain text response with the wrong Content-Type
header.
这篇关于通过AFNetworking 2.0访问DailyMile API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!