2016-09-23 51 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