gpt4 book ai didi

ios - Swift - 使用 alamofire 发布请求

转载 作者:行者123 更新时间:2023-11-28 14:33:38 29 4
gpt4 key购买 nike

我在使用 Alamofire 时遇到了一些问题。我有在 Android 设备上成功运行的服务器功能。一切正常,所有数据都没有问题。但是在 iOS 上,每当我发送超过 2 个参数的发布请求时,我都会从服务器收到错误消息,因此我无法发布数据。我试图将 post 作为字符串放入自定义编码中,因为当我将参数放入 Alamofire.request 方法时,我也遇到了错误,但服务器响应是“unexeption 处理程序中的错误”。因此,由于 String 扩展,我将所有 JSON 更改为字符串,然后将每个“[”和“]”更改为“{”、“}”,但我仍然无法将数据保存到服务器。

在 Swift 中将字典编码并转换为 JSON 如下所示:

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

Swift 中的字典

["calls": ["0": ["call_date": "2018-06-27+11:49:18", "number": "56262621", "sms": "0", "duration": "0", "type": 211]]]

另一方面,Android 上的相同应用程序可以完美处理相同的数据。

用 Java 编码的 JSON 如下所示:

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

Java 中的 JSON

{"calls":{"0":{"number":"852486258965","duration":"0","type":211,"call_date":"2018-06-27 11:53:53","sms":"0"}}}

为什么在 Android 应用程序中一切正常,但在 iOS 上却根本不起作用?有人对此有想法吗?

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)
}
}
}

最佳答案

使用/不使用编码参数以及添加/删除 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)
}
}
}

关于ios - Swift - 使用 alamofire 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51059826/

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