2016-09-23 53 views
0

从一整天的努力,并检查堆栈溢出和几个论坛环节多后,我无法破解它:我想很多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提前!

+0

决不发表您的网址使得其保密安全的目的 – iDeveloper

+0

就删除它,认为它将帮助任何人解决我的问题,但你是对的,谢谢!但迄今没有帮助,我从2天卡住了! – Sunil

+0

你解决了这个问题吗?还是需要更多的帮助? – Ragul

回答

0

这是我写的我的应用程序和定制您的要求

  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或复制线打击,更改代码的“你的.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> 

希望这将有助于

+0

感谢兄弟的帮助!这个对我有用 !! – Sunil