2017-07-03 233 views
0

因此,我使用一個應用程序的API來幫助我存儲數據,以便將數據發送到用戶輸入/選擇的API存儲它,並取回ID以供稍後參考。 這是我使用的一些代碼。使用Alamofire發送POST請求到API

JSON通過API接受

{ 
    "topic":"test shedule", 
    "setdate":"21-09-2017", 
    "scheduledate":"22-09-2017", 
    "settime":"09:00:00", 
    "scheduletime":"10:00:00", 
    "agenda":"hi this is test agenda", 

    "adminname":"name", 
    "type":"meeting", 
    "users": "[{\"category\":\"optional\",\"email\":\"[email protected]\"}]", 
    "adminemail":"[email protected]" 
} 

數據Im使用Alamofire發送\

let parameters: Parameters = [ 
      "topic":topicTextField.text!, 
      "setdate":currentDate as String, 
      "scheduledate":setDate as String, 
      "settime":currentTime as String, 
      "scheduletime":setTime as String, 
      "agenda":agendaTextField.text!, 
      "adminname":"abcname", 
      "type":"meeting", 
      "users" : "\(smembers)", 
      "adminemail":"[email protected]" 
     ] 

     Alamofire.request("\(baseURL)meeting_shedule.php",method: .post, parameters: parameters).responseJSON { response in 
      if let dict = response.result.value as? Dictionary<String, AnyObject> { 
       let topic = dict["topic"] as? String 
       let g_id = dict["g_id"] as? String 

       self.storeMeeting(topic: topic!, g_id: g_id!) 

      } 
     } 

smembers是從字典selectedMembers和字典selectedMembers創建的陣列來自不同的ViewController通過使用準備for segue

var selectedMembers = [String: String]() 
    var smembers = [Any]() 
    var selected = [String: String]() 

if selectedMembers.isEmpty == false { 
      for (key, value) in selectedMembers { 
       selected = ["email":key, "category":value] 
       smembers.append(selected) 
      } 
     } 

的問題

林背離API失敗響應。 我認爲問題是發送「用戶」的API作爲API要求的字符串形式的數據和Im發送數組我甚至試圖通過序列化將smembers轉換爲jsonstring來解決這個問題,但仍然返回失敗。當林發送通過郵局的人這樣要求

接收 enter image description here

API錯誤Im的形象工程。 enter image description here

我不知道如何解決這個問題,如何發送API將接受的數據。在如以下所示的

var selectedMembers = [String: String]() 
    var smembers = [String]() 
    var selected = "" 

if selectedMembers.isEmpty == false { 
      for (key, value) in selectedMembers { 
       selected = String(format: "{\"\\email\\\":\"\\%@\"\\,\"\\category\"\\:\"\\%@\"\\}", arguments: [key,value]) 
       smembers.append(selected) 
      } 
     } 
在烏爾交數據

然後

感謝您的幫助

回答

1

變化邏輯

let parameters: Parameters = [ 
      "topic":topicTextField.text!, 
      "setdate":currentDate as String, 
      "scheduledate":setDate as String, 
      "settime":currentTime as String, 
      "scheduletime":setTime as String, 
      "agenda":agendaTextField.text!, 
      "adminname":"abcname", 
      "type":"meeting", 
      "users" :"[\(smembers.joined(separator: ","))]", 
      "adminemail":"[email protected]" 
     ] 
+0

從不同VC來被一個字典存儲像selectedMembers數據的數據= [電子郵件:類別]。這些數據如何進入您創建的課程中,我不明白這一點? –

+0

顯示我的代碼如何傳遞信息到該API調用 –

+0

哦,它只是一個簡單的準備segue,在ViewControllers之間傳遞selectedMembers看看我在帖子中輸入的最後一個代碼 –