Alamofire.uploadmultipartFormData 与 JSONEncoding.default (Swift)
Alamofire.uploadmultipartFormData with JSONEncoding.default (Swift)
我必须将照片和 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
我知道我可以使用编码:JSONEncoding.default in Alamofire.request 但在使用 Alamofire.uploadmultipartFormData?
时可以使用 JSONEncoding.default
谢谢
您似乎是新手,如果您可以在以后的问题中添加一些您尝试过的代码,那就太好了。无论如何,据我所知,这应该可以通过以下方式实现。我假设密钥 "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)
})
如有任何问题,请联系我。
我必须将照片和 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 我知道我可以使用编码:JSONEncoding.default in Alamofire.request 但在使用 Alamofire.uploadmultipartFormData?
时可以使用 JSONEncoding.default谢谢
您似乎是新手,如果您可以在以后的问题中添加一些您尝试过的代码,那就太好了。无论如何,据我所知,这应该可以通过以下方式实现。我假设密钥 "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)
})
如有任何问题,请联系我。