gpt4 book ai didi

swift - Error Domain=NSCocoaErrorDomain Code=3840 “字符 0 周围的值无效

转载 作者:行者123 更新时间:2023-11-28 06:25:21 26 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于swift - Error Domain=NSCocoaErrorDomain Code=3840 “字符 0 周围的值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com