我有以下自定义类。
class FundingRequest: Codable {
let dispatcherName, loadReference, origin, destination: String
let pickedUp, droppedOff, driverFirstName, driverLastName: String
let grossTruckPay, advancesTaken, fundingMethod, comments: String
let additionalCharges, rateConfirmation, billOfLading, lumperReceipt: [String]
let salesTicket: [String]
enum CodingKeys: String, CodingKey {
case dispatcherName = "DispatcherName"
case loadReference = "LoadReference"
case origin = "Origin"
case destination = "Destination"
case pickedUp = "PickedUp"
case droppedOff = "DroppedOff"
case driverFirstName = "DriverFirstName"
case driverLastName = "DriverLastName"
case grossTruckPay = "GrossTruckPay"
case advancesTaken = "AdvancesTaken"
case fundingMethod = "FundingMethod"
case comments = "Comments"
case additionalCharges = "AdditionalCharges"
case rateConfirmation = "RateConfirmation"
case billOfLading = "BillOfLading"
case lumperReceipt = "LumperReceipt"
case salesTicket = "SalesTicket"
}
init(dispatcherName: String, loadReference: String, origin: String, destination: String, pickedUp: String, droppedOff: String, driverFirstName: String, driverLastName: String, grossTruckPay: String, advancesTaken: String, fundingMethod: String, comments: String, additionalCharges: [String], rateConfirmation: [String], billOfLading: [String], lumperReceipt: [String], salesTicket: [String]) {
self.dispatcherName = dispatcherName
self.loadReference = loadReference
self.origin = origin
self.destination = destination
self.pickedUp = pickedUp
self.droppedOff = droppedOff
self.driverFirstName = driverFirstName
self.driverLastName = driverLastName
self.grossTruckPay = grossTruckPay
self.advancesTaken = advancesTaken
self.fundingMethod = fundingMethod
self.comments = comments
self.additionalCharges = additionalCharges
self.rateConfirmation = rateConfirmation
self.billOfLading = billOfLading
self.lumperReceipt = lumperReceipt
self.salesTicket = salesTicket
}
}
我需要能够将其发送到我的Web服务,该服务期望它在名为JSON的参数中作为JSON。还需要与此一起传递一个称为“代码”的秘密。不确定如何使用alamofire来实现。
这是我到目前为止尝试过的方法,但是没有用,我根本没有做对。
r fundingReqeust = FundingRequest(dispatcherName: txtDispatcherName.text!, loadReference: txtLoadReference.text!, origin: txtOrigin.text!, destination: txtDestination.text!, pickedUp: txtPickedUp.text!, droppedOff: txtDroppedOff.text!, driverFirstName: txtDriverFirstName.text!, driverLastName: txtDriverLastName.text!, grossTruckPay: txtGrossTruckPay.text!, advancesTaken: "0", fundingMethod: "1", comments: txtComments.text!, additionalCharges: additionalChargesString, rateConfirmation: rateConfirmationsURL, billOfLading: billOfLadingsURL, lumperReceipt: lumperReceiptsURL, salesTicket: SaleTicketURL)
let jsonEncoder = JSONEncoder()
let jsonData = try? jsonEncoder.encode(fundingReqeust)
let json:String = String(data: jsonData!, encoding: String.Encoding.utf8)! // the data will be converted to the string
let urlString = "https://xxx.xxx.com"
let parameters: Parameters = ["code": "xxx", "JSON": json]
Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}
}
答案 0 :(得分:0)
尝试以下代码:
func postRatingReviewDetail(ratings:RatingFeedbackModal, completion:@escaping RatingFeedbackCompletionHandler){
let urlString = WEBAPI.rating
guard let url = URL(string: urlString) else { return }
let request = NSMutableURLRequest(url: url)
let authString = "Bearer " + TTUserDefalut.getAccessToken()!
let session = URLSession.shared
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(authString, forHTTPHeaderField: "Authorization")
do {
**let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(ratings)
request.httpBody = jsonData**
}
catch let error{
print("Error", error)
return
}
let _ = session.dataTask(with: request as URLRequest) { data, response, error in
guard let data = data else { return }
do {
let responseData = try JSONDecoder().decode(RatingResponse.self, from: data)
completion(responseData,response,error)
}
catch{
completion(nil,nil,nil)
}
}.resume()
}
答案 1 :(得分:0)
您的服务器是否接受<ListBox x:Name="actorsListBox" ItemsSource="{Binding SelectedItem.Actors, ElementName=actorsListBox}"
SelectedItem="{Binding SelectedActor, Mode=TwoWay}" />
格式的数据?我不确定,但我认为它就像["code": "xxx", "JSON": json]
尝试:
["code": "xxx", "key1": "val1", "key2" : "val2"]