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

我正在尝试使用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 = data

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {

    data, response, error in

    println(response)

    // Your completion handler code here

}

task.resume()


慕桂英4014372
浏览 1073回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP