gpt4 book ai didi

ios - 将 Json 与 post 请求绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 13:03:20 25 4
gpt4 key购买 nike

经过一整天的尝试并检查了堆栈溢出和几个论坛上的许多链接后,我无法破解它:我认为许多 swift 开发人员发现它非常容易,但我无法破解请帮忙

我有一个 url 和一个 JSON 来绑定(bind)并获取 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

但对我来说没有任何作用

感谢所有帮助,提前致谢!!!

最佳答案

这是我为我的应用程序编写的代码,并根据您的请求进行了自定义

         func sendRequest(address: String, method: String, body: Dictionary<String, AnyObject>) {
let url = NSURL(string: address)
let request = NSMutableURLRequest(URL: url!)
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = method

do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions.init(rawValue: 2))
} catch {
// Error handling
print("There was an error while Serializing the body object")
return
}

let session = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in

do {
if error != nil {
print("Error -> \(error)")
return
}

if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? [NSDictionary] {
let result = json
}

} catch {
print("Send request error while Serializing the data object")
return
}

})

session.resume()

}

sendRequest("your.URL.come", method: "POST", body: json)

希望这会有所帮助,还有一件事将 TransportSecurity 添加到您的 info.plist 或复制行并更改“your.URL.come”与您的基本地址

  <key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>your.URL.come</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>

希望这会有所帮助

关于ios - 将 Json 与 post 请求绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39657745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com