ios APNS:错误域= NSCocoaErrorDomain 代码=3840
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27915879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
APNS :Error Domain = NSCocoaErrorDomain Code=3840
提问by rishu1992
Im registering device data with the server, to get push notification. Here it goes the code,
我正在向服务器注册设备数据,以获取推送通知。这是代码,
[NSURLConnection sendAsynchronousRequest: request
queue: _postQueue
completionHandler: ^(NSURLResponse *response, NSData *responseData, NSError *connectionError) {
if (connectionError) {
//
} else {
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData: responseData options: NSJSONReadingMutableContainers error: &error];
}
}];
Im getting error as
我收到错误
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=0x17057f60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Can someone please help to fix the issue?
有人可以帮忙解决这个问题吗?
回答by lxt
The error message is telling you exactly what's wrong: the response from your server doesn't contain valid JSON. Technically, JSON must start with either an array or an object (dictionary). Whatever your server is returning isn't. You can force the JSON to be consumed regardless by using the NSJSONReadingAllowFragments
option.
错误消息准确地告诉您出了什么问题:来自您的服务器的响应不包含有效的 JSON。从技术上讲,JSON 必须以数组或对象(字典)开头。无论您的服务器返回的是什么。无论使用该NSJSONReadingAllowFragments
选项,您都可以强制使用 JSON 。
If after using that option you're stillgetting errors then your server is probably returning malformed JSON (or no JSON at all). Why don't you take a look at the logs from your server to see exactly what you're sending back?
如果使用该选项后仍然出现错误,则您的服务器可能返回格式错误的 JSON(或根本没有 JSON)。你为什么不看看你服务器的日志,看看你发回了什么?
回答by ankit
I was having same issue look what i have done here
我遇到了同样的问题,看看我在这里做了什么
I have change my json parsing method as
我已将我的 json 解析方法更改为
let decodedApps = try JSONDecoder().decode(class.self, from: data!)
here class contain all the key which are present in json data key now you can use decodedApps as dictionary .....which contain key value pair may it will helpful
这里的类包含 json 数据键中存在的所有键,现在您可以使用 decodedApps 作为字典 ..... 其中包含键值对可能会有所帮助