在java
应用程序中,我们通过使用de below代码从REST获得响应。
String response = performPostCall(wmeAPI.vote("" + rowItem.getPoll_id()), has);
在swift中,我正在使用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)")
}
}
如何从此帖后调用中获取响应字符串。
答案 0 :(得分:4)
您需要使用响应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)
}
}
答案 1 :(得分:2)
如果你检查Alamofire
Doc。,已经定义了如何获得响应的信息String以及如何获得响应JSON
响应JSON处理程序
Alamofire.request(.GET, "https://httpbin.org/get")
.responseJSON { response in
debugPrint(response)
}
响应字符串处理程序
Alamofire.request(.GET, "https://httpbin.org/get")
.responseString { response in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
}
链式响应处理程序
Alamofire.request(.GET, "https://httpbin.org/get")
.responseString { response in
print("Response String: \(response.result.value)")
}
.responseJSON { response in
print("Response JSON: \(response.result.value)")
}