在 java
应用程序中,我们使用下面的代码从 REST 获取字符串形式的响应。
String response = performPostCall(wmeAPI.vote("" + rowItem.getPoll_id()), has);
我正在用 Alamofire
快速调用
我打了邮寄电话
Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseString
{ response in switch response.result {
case .Success(let JSON):
print("Success \(JSON)")
case .Failure(let error):
print("Request failed with error: \(error)")
}
}
如何从这个 post 调用中获取响应字符串。
最佳答案
您需要使用响应 JSON
因此,将 responseString
更改为 responseJSON
例如:
Alamofire.request(.GET, "YOUR_URL").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
}
}
对于 POST 调用:
Alamofire.request(.POST, "YOUR_URL", parameters: nil, encoding: ParameterEncoding.JSON, headers: nil).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
print(resJson)
}
if responseObject.result.isFailure {
let error : NSError = responseObject.result.error!
print(error)
}
}
关于ios - 读取来自 Json Post Call Swift 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809603/