问题
I am using Alamofire 4.0, While making network call I am getting below error during the app review so that my app is getting rejected. But for me and my client it's working fine we are never getting below error
JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format.
We also checked that the service call is not hitting the server(so not getting any help from their side), Please find the code snippet
manager.request(requestURL, method: .post, parameters: param as? Parameters, encoding: URLEncoding.methodDependent, headers: nil).responseJSON { (responseJson) in
})
By putting various types of parameters like special character, nil etc.. we did not able to reproduce the error from our side. But during app review we are getting this error. Please help me!!!
Parameter:
回答1:
var params = [String:Any]()
params["stripeToken"] = self.stripeToken
params["currency"] = "gbp"
Alamofire.request(urlString, method: .post, parameters: params, encoding: JSONEncoding.default, headers: ["Content-Type":"application/json"]).responseJSON { (response) in
switch response.result {
case .success:
print("it's success")
case .failure(let error):
print("\n\n===========Error===========")
print("Error Code: \(error._code)")
print("Error Messsage: \(error.localizedDescription)")
if let data = response.data, let str = String(data: data, encoding: String.Encoding.utf8){
print("Server Error: " + str)
}
debugPrint(error as Any)
print("===========================\n\n")
}// end of switch
} //end of Alamofire
回答2:
First try to print in the log if you get any response like this:
Alamofire.request(url, method: .post, parameters: parameter).responseJSON { (response) in
//For Network Error
guard response.result.error == nil else {
print(response.result.error!)
return
}
if let value = response.result.value {
let json = JSON(value)
print("myJSON are: \(json)"
}
}
If this does have any error than tell the server team to check. Also check the app transport security in the plist .
来源:https://stackoverflow.com/questions/54674453/json-could-not-be-serialized-because-of-error