我有一个将一些数据发布到服务器的 url。如何使用 URL 中提到的所有参数将其转换为 alamofire 发布请求方法
Posted
技术标签:
【中文标题】我有一个将一些数据发布到服务器的 url。如何使用 URL 中提到的所有参数将其转换为 alamofire 发布请求方法【英文标题】:I have a url which posts some data to the server. How do i convert it to alamofire post request method with all parameters mentioned in URL 【发布时间】:2017-03-24 06:10:13 【问题描述】:let url = "(InsertAsset)?customerID=(vmanagerappDelegate.cust_Id!)&assetTag=(self.txt_AssetTag.text! as String)&modelID=(modelObject)&statusID=(statusObject)&serial=(self.txt_Serial.text! as String)&assetName=(self.txt_AssetName.text! as String)&purchaseDate=(selectedPurchaseDate)&supplierID=(supplierObject)&purchaseCost=(purchaseCostObject)&warranty=(self.txt_Warranty.text! as String)¬es=(notesObject)&locationID=( locationObject)&ownerID=(ownerObject)&addUserID=(vmanagerappDelegate.userId as String)&saleEndDate=(self.txt_Sale_End_Date.text! as String)&softwareMaintainenceEndDate=(self.txt_Software_Maintainence_End_Date.text! as String)&supportEndDate=(self.txt_Support_End_Date.text! as String)&MacAddress=(self.txt_Mac_Address.text! as String)&sitekey=testing"
【问题讨论】:
你应该用以上所有参数制作一个字典并将其发送到 Alamo.request 向我们展示您正在尝试的当前 Alamofire 代码? dev.myvmanager.com/vManagerMobileWebService/api/Asset/Insert 这是url,上面是参数。请告诉我如何使用上述 url 和参数进行发布请求 让参数:[String : String] = ["customerID" : vmanagerappDelegate.cust_Id, "assetTag" : self.txt_AssetTag.text!, "modelID" : modelObject, "statusID" : statusObject, "串行”:self.txt_Serial.text!作为字符串等像这样我已经为参数编写了代码...... 【参考方案1】:您好,您可以创建如下字典以在 Alamofire 中将参数作为字典发送:
var dicParam = Dictionary<String,AnyObject>()
dicParam[“customerID”] = vmanagerappDelegate.cust_Id!
dicParam[“assetTag”] = self.txt_AssetTag.text! as String
dicParam["modelID"] = modelObject
在 Alamofire 中,将上面的字典作为参数与您的 URL 一起传递,如下所示:
Alamofire.request("Your URL", method: .post, parameters: dicParam, encoding: URLEncoding(destination: .queryString), headers: nil).responseJSON
response in
switch response.result
case .success:
print(response)
break
case .failure(let error):
print(error)
【讨论】:
【参考方案2】:尝试创建字典
let params:Dictionary<String, AnyObject> = ["customerID":vmanagerappDelegate.cust_Id!,"assetTag":self.txt_AssetTag.text,"modelID":modelObject] //similarly add all params to Dict
如果你想添加一些标题,也为它们创建字典 Header 并传入Alamofire.request(.POST,url,parameters: params,headers:Header,encoding: .JSON)
post 请求示例
func remotePOSTServiceWithParameters(urlString : String , params : Dictionary<String, AnyObject> , callback:(data: Dictionary<String, AnyObject>?, error: NSError? ) -> Void)
print("Request POST URL:\(urlString) PARAMS:\(params)")
Alamofire.request(.POST,urlString,parameters: params,encoding: .JSON)
.validate()
.responseJSON
response in
guard response.result.error == nil else
print("Error for POST :\(urlString):\(response.result.error!)")
//
callback(data: nil , error: response.result.error! )
return
if let value = response.result.value
print("JSON: \(value)")
if let result = value as? Dictionary<String, AnyObject>
print("Response for POST :\(urlString):\(value)")
callback(data:result , error: nil )
【讨论】:
以上是关于我有一个将一些数据发布到服务器的 url。如何使用 URL 中提到的所有参数将其转换为 alamofire 发布请求方法的主要内容,如果未能解决你的问题,请参考以下文章
如何仅将一些选定的 Json 数组和 Json 对象从 Url 解析到 android 设备