xcode 错误域=NSCocoaErrorDomain 代码=3840“字符 0 周围的值无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42085145/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:46:18  来源:igfitidea点击:

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0

swiftxcodealamofire

提问by Egle Matutyte

Hi I am new at swift and I got problem, I made a request to server with post method and I get response with good Json, after that I am makeing another request with get method but I get this error.

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

Error:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})

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

Parameters for request:

请求参数:

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)
    })
}

Request:

要求:

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 response:

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.}))

回答by Mehedi Hasan

The Error saying that the response from server is not a valid JSON string. Can you try responseString instead of responseJSON like

错误表明来自服务器的响应不是有效的 JSON 字符串。你能试试 responseString 而不是 responseJSON 吗

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

See the Xcode debugger output & change it according to your need.

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

回答by 2rahulsk

I got the same error and i fixed it when I added encoding : JSONEncoding.default in the HTTPHeader. It could also be that the response from the server is an invalid JSON. You could get in touch with the server team and check if the output is of the correct format.

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

回答by Hamest Tadevosyan

The size of the image is too much. The allowed memory size of 134,217,728 bytes is exceeded. You tried to allocate 48,771,073 bytes on function:

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

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

Instead of:

代替:

image = info[UIImagePickerControllerOriginalImage] 

Use:

用:

image = info[UIImagePickerControllerEditedImage]