2016-07-22 64 views
0

我正在使用AlamoFire在Google Cloud Prediction中對我的某個模型進行POST查詢。無論何時我發送請求,我都會返回一個錯誤,指出:此API不支持解析表單編碼輸入。
經過一番研究,我發現我需要將我的Content-Type HTTP標頭設置爲「application/json」。希望你能在找到我的請求時找到我錯過的東西。這裏是我的代碼:AlamoFire請求Google Cloud Prediction API iOS解析錯誤

let parameters = [ 
     "access_token" : accessToken, 
     "input": [ 
      "csvInstance": [ 
       "This is very positive" 
      ] 

     ] 
] 
Alamofire.Manager.sharedInstance.session.configuration 
      .HTTPAdditionalHeaders?.updateValue("application/json", 
               forKey: "Accept") 
Alamofire.Manager.sharedInstance.session.configuration 
      .HTTPAdditionalHeaders?.updateValue("application/json", 
               forKey: "Content-Type") 
Alamofire.request(.POST, "https://www.googleapis.com/prediction/v1.6/projects/mailanalysis-1378/trainedmodels/10kTweetData/predict", parameters: parameters).responseJSON { (response) in 
     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
      //print("refresh token = " + auth.accessToken) 
     } 
} 
+0

Alamofire爲您處理'應用/ json'一旦你在方法'requestJSON'中使用JSON序列化器,你應該使用它而不是正常的請求 –

+0

@VictorSigler你是什麼意思的方法requestJSON?我如何稱呼它?我似乎無法將其作爲Alamofire的方法之一 – user3798602

+0

抱歉,我的錯誤是['responseJSON'](https://github.com/Alamofire/Alamofire#response-json-handler) –

回答

0

萬一有人還在尋找一個答案,我設法從我的iOS客戶端訪問GooglePredictionAPI沒有Alamofire:

var accessToken: String? 

    GIDSignIn.sharedInstance().currentUser.authentication.getTokensWithHandler { (authentication, error) in 

     if let err = error { 
      print(err) 
     } else { 
      if let auth = authentication { 
       accessToken = auth.accessToken 
      } 
     } 
    } 

    if let accTok = accessToken { 

     let parameters = [ 
         "input": [ 
          "csvInstance": [ 
           0.9, 
           0.14, 
           -0.41, 
           1.61, 
           -1.67, 
           1.57, 
           -0.14, 
           1.15, 
           0.26, 
           -1.52, 
           -1.57, 
           3.65 
          ] 

         ] 
        ] 

     let url = NSURL(string: "https://www.googleapis.com/prediction/v1.6/projects/ExermotePredictionAPI/trainedmodels/getExercise/predict") 

     let session = URLSession.shared 

     let request = NSMutableURLRequest(url: url! as URL) 
     request.httpMethod = "POST" //set http method as POST 

     do { 
      request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) 

     } catch let error { 
      print(error.localizedDescription) 
     } 

     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 
     request.addValue("Bearer \(accTok)", forHTTPHeaderField: "Authorization") 

     let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in 

      guard error == nil else { 
       return 
      } 

      guard let data = data else { 
       return 
      } 

      do { 
       if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] { 
        print(json) 
       } 

      } catch let error { 
       print(error.localizedDescription) 
      } 

     }) 

     task.resume() 
    }