Alamofire.uploadmultipartFormData 与 JSONEncoding.default (Swift)

Posted

技术标签:

【中文标题】Alamofire.uploadmultipartFormData 与 JSONEncoding.default (Swift)【英文标题】:Alamofire.uploadmultipartFormData with JSONEncoding.default (Swift) 【发布时间】:2020-09-27 11:13:18 【问题描述】:

我必须将照片和 json 发送到服务器。 我的 json 是:

"anticorona":"Anti_Covid","time":"Time","navigateds":["collection_public_key":"Origin_Station.collection_public_key","station_public_key":"Origin_Station.public_key","station_public_key":"Des_Station.public_key","collection_public_key":"Des_Station.collection_public_key"],"seats":"Seats","date":"Date"

如何使用 Alamofire.uploadmultipartFormData 发送此 json 我知道我可以在 Alamofire.request 中使用 encoding: JSONEncoding.default 但在使用 Alamofire.uploadmultipartFormData 时可以使用 JSONEncoding.default?

谢谢

【问题讨论】:

从请求中移除此默认 json 编码,因为它不适用于数据请求 【参考方案1】:

您似乎是新人,如果您可以添加一些您在以后的问题中尝试过的代码,那就太好了。无论如何,据我所知,这应该可以通过以下方式实现。我假设密钥"navigateds" 保持不变。否则,您还可以检查该值(在 for-in-loop 中)是否也是一个数组:

// set parameters for request
let antiCoronaParameters: Parameters = [
    "anticorona"   : "Anti_Covid",
    "time":"Time",
    "navigateds":[
      ["collection_public_key":"Origin_Station.collection_public_key", "station_public_key":"Origin_Station.public_key"],
      ["station_public_key":"Des_Station.public_key", "collection_public_key":"Des_Station.collection_public_key"]
    ],
    "seats":"Seats",
    "date":"Date"
]

let upload = AF.upload(multipartFormData:  (formData) in

    // I would append file data here first
    
    for (key, value) in antiCoronaParameters 
        if key == "navigateds" 
            do 
                let arrayData = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
                formData.append(arrayData, withName: key)
             catch 
                print("could not append array, failed with error:", error)
            
         else if let string = value as? String, let stringData = string.data(using: String.Encoding.utf8, allowLossyConversion: false) 
            formData.append(stringData, withName: key)
         else 
            print("could not append some data in parameters")
        
    
    
, to: "https://www.yourURLhere.com/link.php", method: .post).validate()

upload.responseString  (responseString) in
    print(responseString)

此答案基于this 问题。将来,我建议您尝试大量与您的问题相关的关键字。最初,搜索问题会花费更多时间,但我相信你很快就会掌握它。下次也许再多尝试一点;)

但是,正如您已经提到的,还有另一种方法,如果您想检查一下,我已经为您准备好了。你基本上已经提到了它,除非有很好的理由,否则我总是更喜欢它而不是 upload

// set parameters for request
let antiCoronaParameters: Parameters = [
    "anticorona"   : "Anti_Covid",
    "time":"Time",
    "navigateds":[
      ["collection_public_key":"Origin_Station.collection_public_key", "station_public_key":"Origin_Station.public_key"],
      ["station_public_key":"Des_Station.public_key", "collection_public_key":"Des_Station.collection_public_key"]
    ],
    "seats":"Seats",
    "date":"Date"
]

// request with json encoded parameters (e.g. sending to php)
let antiCoronaRequest = AF.request("https://www.yourURLhere.com/link.php", method: .post, parameters: antiCoronaParameters, encoding: JSONEncoding.default).validate()
    
antiCoronaRequest.responseString(completionHandler:  (response) in
    print(response)
)

如果有任何问题,请联系我。

【讨论】:

以上是关于Alamofire.uploadmultipartFormData 与 JSONEncoding.default (Swift)的主要内容,如果未能解决你的问题,请参考以下文章