從一整天的努力,並檢查堆棧溢出和幾個論壇環節多後,我無法破解它:我想很多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