使用Swift的NSDictionary值的Post方法
我对Swift
完全陌生。我需要点击Post Method webservice NSDictionary
参数&获取JSON响应。我尝试使用Alamofire
&也NSMutableUrlRequest
。似乎没有任何锻炼对我来说。我要么从服务器得到'JSON文本没有以数组或对象开始,并且选项允许片段未设置'错误或'未定义变量'响应。当我尝试使用Objective-C
时,相同的服务正常工作。正如我刚才所说,我是全新的Swift
&需要您的帮助。使用Swift的NSDictionary值的Post方法
我的基本网址:http://myofficeit.in/bizfeed/webservices/client.php
参数我想通行证:
参数=
{
UserName = xyz;
deviceModel = iPhone;
deviceToken = "949264bc cd9c6c851ee64cc74db9078770dd7d971618ec20ce91d2e6eb9f155e";
emailid = "[email protected]";
location = Asia;
userMobileNo = 1234567890;
};
functionName = register;
我用来打的服务代码是:http://pastebin.com/aaT4uhS7
感谢
let param: [String:AnyObject] = [
"UserName": iPhone,
"deviceToken": "949264bc cd9c6c851ee64cc74db9078770dd7d971618ec20ce91d2e6eb9f155e",
"emailid": "[email protected]",
"location": Asia,
"userMobileNo": 1234567890
]
Alamofire.request(.POST, "http://myofficeit.in/bizfeed/webservices/client.php/register", parameters: param).responseJSON { (req, res, json, error) in
print(req)
print(res)
print(json)
print(error)
}
为Alamofire样本请求
宽得像你的问题是,宽阔的将是我的答案:
做的第一件事情,就是要获得关于Web服务的一个明确的想法API,它也需要HTTP协议的基本知识。所以,你需要了解的是,服务器在HTTP术语中所期望的。
你最终会发现,服务器将如何期待它的“参数”。请注意,HTTP协议中没有像“参数”这样的术语。所以,你需要将它们映射到HTTP协议提供的东西。
最有可能的是,在POST请求中,“参数”作为HTTP消息的主体被传送,作为内容类型,其为application/x-www-form-urlencoded
,multipart/form-data
或application/json
。
根据服务器的需求,并与您的HTTP和NSURLSession
,NSURLComponents
等的基本知识,您撰写的URL和请求的主体,集Content-Type
头和其他可能的头,你准备好去。
这最终看起来如何在使用Alamofire的@AnbyKarthik的回答中给出,以及组成POST请求的命令,其参数在内容类型为x-www-form-urlencoded
的主体中发送。
Thanks dude。现在有了一个想法。 –
我正在使用最新版本的AlamoFire。这实际上会引发''Contexutal闭包类型响应需要1个参数,但4个在闭包体中使用“ – Vignesh
@Vignesh - 检查此链接兄弟http://stackoverflow.com/questions/33384342/contextual-type-for-closure-argument- list-expect-1-argument-but-4-were-specifi –
Thanks Bro。我现在收到这个错误:Request failed with error:Error Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效。” –