2016-07-22 43 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() 
    }