【问题标题】:Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0错误域 = NSCocoaErrorDomain 代码 = 3840 “字符 0 周围的值无效
【发布时间】:2017-06-24 10:04:07
【问题描述】:

您好,我是 swift 新手,但遇到了问题,我使用 post 方法向服务器发出请求,我得到了良好 Json 的响应,之后我使用 get 方法发出另一个请求,但出现此错误。

错误: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "字符 0 附近的值无效。" UserInfo={NSDebugDescription=字符 0 附近的值无效})

请求参数:

static func getInformationFromConfig(token: String, config: String, section : String, option: String) -> [String:Any] {

        let getInformationFromConfigparam: [String : Any] = ["jsonrpc": "2.0",
                                      "id": 1,
                                      "method": "call",
                                      "params": [ token, "uci", "get", [ "config": config, "section": section, "option": option]]
    ]

    return getInformationFromConfigparam
}



 public func device(token: String, loginCompletion: @escaping (Any) -> ()) {
    let deviceinfo = JsonRequests.getInformationFromConfig(token: token, config: "wireless", section: "@wifi-iface[0]", option: "mode")
    makeWebServiceCall(urlAddress: URL, requestMethod: .get, params: deviceinfo, completion: { (JSON : Any) in
        loginCompletion(JSON)
    })
}

请求:

private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {


    Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON{ response in


        switch response.result {
        case .success(let value):

            let json = JSON(value)

            if let jsonData = response.result.value {

                completion(jsonData)
            }


        case .failure(let error):

                completion("Failure Response: \(error)")

ResponseString 响应:

 [Request]: GET http://192.168.1.1/ubus
[Response]: <NSHTTPURLResponse: 0x60000003c4a0> { URL: http://192.168.1.1/ubus } { status code: 400, headers {
Connection = "Keep-Alive";
"Content-Type" = "text/html";
"Keep-Alive" = "timeout=20";
"Transfer-Encoding" = Identity;
} }
[Data]: 35 bytes
    [Result]: FAILURE:     responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

【问题讨论】:

标签: swift xcode alamofire


【解决方案1】:

表示来自服务器的响应不是有效的 JSON 字符串的错误。你可以试试 responseString 而不是 responseJSON 之类的

Alamofire.request(urlAddress, method: requestMethod, parameters: params).responseString{ response in
    debugPrint(response)
}

查看 Xcode 调试器输出并根据需要进行更改。

【讨论】:

  • @EgleMatutyte,响应 URL 为空。这意味着您使用 .get 方法发送的参数有问题。
  • 我发现它是错误的令牌,但现在我修复它并收到状态码 400 错误,再次更新响应
【解决方案2】:

图片尺寸过大。超出了允许的内存大小 134,217,728 字节。您试图在函数上分配 48,771,073 字节:

imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

代替:

image = info[UIImagePickerControllerOriginalImage] 

用途:

image = info[UIImagePickerControllerEditedImage]

【讨论】:

  • 你能在这个答案中添加一些格式吗?另外,分成几个句子会使这个答案更清楚。
  • 答案也与问题无关
【解决方案3】:

我遇到了同样的错误,我在添加编码时修复了它:HTTPHeader 中的 JSONEncoding.default。也可能是来自服务器的响应是无效的 JSON。您可以联系服务器团队并检查输出的格式是否正确。

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 2018-02-20
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2014-11-17
    • 2017-06-16
    相关资源
    最近更新 更多