从一整天的努力,并检查堆栈溢出和几个论坛环节多后,我无法破解它:我想很多swift
开发商发现它很容易,但我不能破解它,请帮助绑定的Json与POST请求
我有一个网址和一个JSON绑定,并获得JSON响应
要发布的网址:http://myurl/myurl.com.......//
JSON请求:
{
"BookingId": "1501433021",
"ProductType": "0",
"Client": "1",
"OriginAirport": {
"CityCode": "NYC",
"CityName": "New York",
"AirportCode": "NYC",
"AirportName": "New York City All Airports",
"Country": "US",
"Terminal": ""
},
"DestinationAirport": {
"CityCode": "LON",
"CityName": "London",
"AirportCode": "LON",
"AirportName": "London All Airports",
"Country": "GB",
"Terminal": ""
},
"TravelDate": "2016-10-19T05:07:57.865-0400 ",
"ReturnDate": "2016-10-21T05:08:02.832-0400 ",
"SearchDirectFlight": false,
"FlexibleSearch": false,
"TripType": 2,
"Adults": 1,
"Children": 0,
"Infants": 0,
"CabinType": 1,
"SearchReturnFlight": true,
"Airline": "",
"CurrencyCode": "USD",
"SiteId": "LookupFare"
}
mycode的(此代码是从一些地方,我只是试图让这对我的工作拷贝),这显然不是为我工作
import UIKit
class SearchFlightsVC: UIViewController{
override func viewDidLoad() {
print("vdfvdfvdf")
// prepare json data
let json = ["BookingId": "1501433021",
"ProductType": "0",
"Client": "1",
"OriginAirport": [
"CityCode": "CLT",
"CityName": "Charlotte",
"AirportCode": "CLT",
"AirportName": "Douglas Intl",
"Country": "US",
"Terminal": ""
],
"DestinationAirport": [
"CityCode": "YTO",
"CityName": "Toronto",
"AirportCode": "YTO",
"AirportName": "Toronto All Airports",
"Country": "CA",
"Terminal": ""
],
"TravelDate": "2016-10-19T05:07:57.865-0400",
"ReturnDate": "2016-10-21T05:08:02.832-0400",
"SearchDirectFlight": false,
"FlexibleSearch": false,
"TripType": 2,
"Adults": 1,
"Children": 0,
"Infants": 0,
"CabinType": 1,
"SearchReturnFlight": true,
"Airline": "",
"CurrencyCode": "USD",
"SiteId": "LookupFare" ]
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
print(jsonData)
// create post request
let url = NSURL(string: "http://myurl/myurl.com.......//")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
// insert json data to the request
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = jsonData
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
print(response)
if error != nil{
print("Error -> \(error)")
return
}
do {
let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print("Result -> \(result)")
} catch {
print("Error -> \(error)")
}
}
//task.resume()
//return task
} catch {
print(error)
}
}
}
我已经签
How to create and send the json data to server using swift language
HTTP Request in Swift with POST method
但是没有什么适合我的
ALL HELP IS Appreciated Thanks in提前!
决不发表您的网址使得其保密安全的目的 – iDeveloper
就删除它,认为它将帮助任何人解决我的问题,但你是对的,谢谢!但迄今没有帮助,我从2天卡住了! – Sunil
你解决了这个问题吗?还是需要更多的帮助? – Ragul