2

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.}))
Egle Matutyte
  • 225
  • 1
  • 8
  • 18
  • Possible duplicate of [Alamofire invalid value around character 0](http://stackoverflow.com/questions/32355850/alamofire-invalid-value-around-character-0) – Ahmad F Feb 07 '17 at 08:55

3 Answers3

6

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.

Mehedi Hasan
  • 329
  • 2
  • 7
0

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.

2rahulsk
  • 488
  • 4
  • 10
-6

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]
Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99