2016-10-17 116 views
1

我試圖從用戶發送輸入到一個php腳本,讓它最終傳遞給一個SQL服務器,大多數的代碼運行但有提交數據的問題。swift 3 http請求

@IBAction func submit(_ sender: AnyObject) { 

    let requestURL = URL(string: "*****") 

    let request = NSMutableURLRequest(url:requestURL!) 

    request.httpMethod = "POST" 

    let song=txt1.text! 
    let artist=txt2.text! 
    let album=txt3.text! 
    let year=txt4.text! 
    let genre=txt5.text! 


    let songPost = "song=" + (song as String) 
    let artistPost = "&artist=" + (artist as String) 
    let albumPost = "&album=" + (album as String) 
    let yearPost = "&year=" + (year as String) 
    let genrePost = "&genre=" + (genre as String) 

    request.httpBody = songPost.data(using: String.Encoding.utf8); 
    request.httpBody = artistPost.data(using: String.Encoding.utf8); 
    request.httpBody = albumPost.data(using: String.Encoding.utf8); 
    request.httpBody = yearPost.data(using: String.Encoding.utf8); 
    request.httpBody = genrePost.data(using: String.Encoding.utf8); 
--->>let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else {             // check for fundamental networking error 
      print("error=\(error)") 
      print(response) 

      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 
     } 

     let responseString = String(data: data, encoding: .utf8) 
     print("responseString = \(responseString)") 
    } 
    task.resume() 

存在urlsession.shared.datatask代碼行問題。編譯器錯誤說「ambigious引用member'dataTask(with:completionhandler :)」

什麼可以讓此代碼工作,以及如何驗證此信息是通過應用程序?

回答

2

您收到該錯誤消息的原因是因爲您通過了NSMutableURLRequest,其中需要URLRequest

更改該行:

let request = NSMutableURLRequest(url:requestURL!) 

這樣:

var request = URLRequest(url:requestURL!) 

應該修復它。

但我建議多一點修正,使您的請求成功發送到服務器:

let requestURL = URL(string: "*****") 

    //You should use `URLRequest` in Swift 3, mutability is represented by `var` 
    var request = URLRequest(url:requestURL!) 

    request.httpMethod = "POST" 

    //UITextField.text can be nil, you should treat nil cases 
    //(Generally avoid using forced unwrapping `!` as far as you can.) 
    let song = txt1.text ?? "" 
    let artist = txt2.text ?? "" 
    let album = txt3.text ?? "" 
    let year = txt4.text ?? "" 
    let genre = txt5.text ?? "" 

    //`song`,... are all Strings, you have no need to add `as String` 
    let songPost = "song=" + song 
    let artistPost = "&artist=" + artist 
    let albumPost = "&album=" + album 
    let yearPost = "&year=" + year 
    let genrePost = "&genre=" + genre 

    //You need to make a single data containing all params 
    //(Creating a concatenated String and getting `data` later would be another way.) 
    var data = Data() 
    data.append(songPost.data(using: String.Encoding.utf8)!) 
    data.append(artistPost.data(using: String.Encoding.utf8)!) 
    data.append(albumPost.data(using: String.Encoding.utf8)!) 
    data.append(yearPost.data(using: String.Encoding.utf8)!) 
    data.append(genrePost.data(using: String.Encoding.utf8)!) 
    request.httpBody = data 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     ... 
+0

那真棒!我想我正在用swift 2找到答案,並將其混合到我的代碼中,我無法將它們分開。它不顯示任何錯誤和運行,但是當我點擊提交應用程序時,有一個線程1:斷點1.1錯誤,其綠色也不是紅色,它使用var request = URLREQUEST在線上正確使用。什麼是斷點? – xteetsx

+0

也即時得到一個錯誤,說無效重新宣佈的'viewController'出現的地方,我沒有任何具有相同名稱的功能 – xteetsx

+0

好的答案。你也可以用'.utf8'替換所有'String.Encoding.utf8'引用。 – Rob