2017-02-13 82 views
0

我想通过key=>value发送一些参数,如volley android(类似于form html)的库。如何通过key => value发送值作为post方法swift

我dont't要发送的值作为JSON参数。

我该怎么办?

Map<String, String> params = new HashMap<>(); 
     // the POST parameters: 
     params.put("site", "code"); 
     params.put("network", "tutsplus"); 
+1

请出示您试图代码 –

+0

我是一个Android开发者我不知道如何发送像没有json值的参数值。在抽象的Map params = new HashMap <>(); //的POST参数: params.put( “点”, “代码”); params.put(“network”,“tutsplus”); ' –

+1

可以使用AFNetworking,Alamofire或NSUrlsession它 –

回答

0

您应该使用Dictionary和串行化处理的字典作为JSON并将其设置为HTTPBody

在凌空库

let parameters = ["site":"code", "network":"tutsplus"] as Dictionary<String, String> 
let request = URLRequest(url: URL(string:YOURURL)!) 
let session = URLSession.shared 
request.httpMethod = "POST" 

request.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: []) 

另一种方法是:

request.httpBody = "site=code&network=tutsplus" 
+0

为什么要使用JSON? POST查询参数已经像字典一样。 – user28434

+0

作为大厅请求的帖子正文结尾。 – hasan83

+0

当您使用serialisable和字典时,您不必担心文本转义。包括空格和反斜杠等 – hasan83

-1

你可以只设置键和值在httpBody,一S,从而:

request.httpBody = "key=value" 

你侃甚至通过添加“&”增加更多的按键,有点像你通常会在做GET

request.httpBody = "key=value&otherKey=otherValue"