为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Swift中使用JSON主体发出HTTP Post请求

如何在Swift中使用JSON主体发出HTTP Post请求

慕桂英4014372 2019-08-31 11:14:55
我正在尝试使用JSON正文发出HTTP post请求:如何能够将NSdictionnary添加到HTTP请求主体。这是我的代码,它似乎无法正常工作。var entry1 = Response(IdQuestion: 6510,IdProposition: 10,Time: 30)var entry2 = Response(IdQuestion: 8284,IdProposition: 10,Time: 30)Responses.append(entry1)Responses.append(entry2)let list = Responses.map { $0.asDictionary }let json = ["List":list,"IdSurvey":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]let data : NSData = NSKeyedArchiver.archivedDataWithRootObject(json)NSJSONSerialization.isValidJSONObject(json)let myURL = NSURL(string: "http://www.myserver.com")!let request = NSMutableURLRequest(URL: myURL)request.HTTPMethod = "POST" request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")    request.setValue("application/json", forHTTPHeaderField: "Accept")request.HTTPBody = datalet task = NSURLSession.sharedSession().dataTaskWithRequest(request) {    data, response, error in    println(response)    // Your completion handler code here}task.resume()
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 1095 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信