问题
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.
Error: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 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:
[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.}))
回答1:
The Error saying that the response from server is not a valid JSON string. Can you try responseString instead of responseJSON like
Alamofire.request(urlAddress, method: requestMethod, parameters: params).responseString{ response in
debugPrint(response)
}
See the Xcode debugger output & change it according to your need.
回答2:
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.
回答3:
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:
imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
Instead of:
image = info[UIImagePickerControllerOriginalImage]
Use:
image = info[UIImagePickerControllerEditedImage]
来源:https://stackoverflow.com/questions/42085145/error-domain-nscocoaerrordomain-code-3840-invalid-value-around-character-0