Reputation: 486
I'm having a little trouble with Alamofire. I have server functions that successfully working on android devices. Everything works fine and all data working without problems. But on iOS everytime when I send post request with more than 2 parameters I got an error from server so I can't post data. I tried to put post as a string in custom encoding, because when I put parameters into Alamofire.request method I also got an error but then server response is "error in unexeption handler". So I changed all JSON into string thanks to String extension, then I change every "[" and "]" to "{", "}", but still I can't save data to server.
Encoded Dictionary and changed into JSON in Swift looks like this:
calls=%7B%220%22%3A%7B%22call%5Fdate%22%3A%222018%2D06%2D27%2B11%3A30%3A46%22%2C%22number%22%3A%22462462432%22%2C%22sms%22%3A%220%22%2C%22duration%22%3A%220%22%2C%22type%22%3A211%7D%7D
Dictionary in Swift
["calls": ["0": ["call_date": "2018-06-27+11:49:18", "number": "56262621", "sms": "0", "duration": "0", "type": 211]]]
On the other hand same application on Android works perfect with the same data.
Encoded JSON in Java looks like this:
calls=%7B%220%22%3A%7B%22number%22%3A%22852486258965%22%2C%22duration%22%3A%220%22%2C%22type%22%3A211%2C%22call_date%22%3A%222018-06-27+11%3A53%3A53%22%2C%22sms%22%3A%220%22%7D%7D
JSON in Java
{"calls":{"0":{"number":"852486258965","duration":"0","type":211,"call_date":"2018-06-27 11:53:53","sms":"0"}}}
Why in Android App everything works but on iOS it doesn't work at all? Does anyone have an idea for that?
Function in Swift
func sendStoreCalls(parameters: Dictionary<Int, Dictionary<String, Any>>) {
var params: Parameters = [:]
var par: Parameters = [:]
var pa: [String:Any] = [:]
var i = 0
for (_, param) in parameters {
if param["isSend"] as! Int == 0 {
par["duration"] = param["callTime"]
par["sms"] = "0"
let callDate: String = param["callDate"] as! String
par["type"] = param["callType"]
par["call_date"] = callDate.replacingOccurrences(of: " ", with: "+", options: .literal, range: nil)
if param["callNumber"] as! String != "" {
par["number"] = param["callNumber"]
}
params[String(i)] = par
i += 1
}
}
pa["calls"] = params
let token: String = SharedPreferences.getPrefToken()!
let bufferedParams = pa.queryParameters
let transformedPar = bufferedParams.replacingOccurrences(of: "%5B", with: "%7B", options: .literal, range: nil).replacingOccurrences(of: "%5D", with: "%7D", options: .literal, range: nil)
let trn = transformedPar.replacingOccurrences(of: "%C2A0", with: "", options: .literal, range: nil).replacingOccurrences(of: "%20", with: "", options: .literal, range: nil)
let url = "https://url.com?api_token=\(token)"
Alamofire.request(url, method: .post, encoding: JSONStringArrayEncoding.init(string: trn))
.responseJSON {
response in
print(NSString(data: (response.request?.httpBody)!, encoding: String.Encoding.utf8.rawValue)!)
switch response.result {
case .success(let value):
let json = JSON(value)
print(json["message"].rawString()!)
case .failure(let error):
if let data = response.data {
print("ERROR!!!")
print(String(data: data, encoding: String.Encoding.utf8)!)
}
print(error)
}
}
}
Upvotes: 0
Views: 108
Reputation: 2092
Try this method with/without encoding parameter as well as by adding/Removing header ["Content-Type":"application/json"]
func request(_ method: HTTPMethod
, _ URLString: String
, parameters: [String : AnyObject]? = [:]
, headers: [String : String]? = [:]
, onView: UIView?, vc: UIViewController, completion:@escaping (Any?) -> Void
, failure: @escaping (Error?) -> Void) {
Alamofire.request(URLString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in
switch response.result {
case .success:
completion(response.result.value!)
case .failure(let error):
failure(error)
guard error.localizedDescription == JSON_COULDNOT_SERIALISED else {
AppUtil().showMessage((error.localizedDescription), messageTitle: EMPTY_STRING, buttonTitle: OK, vc: vc)
return
}
// AppUtil.showMessage(SOMETHING_WNET_WRONG, messageTitle: EMPTY_STRING, buttonTitle: OK)
}
}
}
Upvotes: 1