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

时间:2017-02-07 08:36:47

标签: swift xcode alamofire

嗨我是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.}))

3 个答案:

答案 0 :(得分:4)

错误说服务器的响应不是有效的JSON字符串。你可以尝试使用responseString而不是像

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

参见Xcode调试器输出&amp;根据你的需要改变它。

答案 1 :(得分:0)

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

答案 2 :(得分:-3)

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

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

而不是:

image = info[UIImagePickerControllerOriginalImage] 

使用:

image = info[UIImagePickerControllerEditedImage]