使用Alamofire Swift将JSON结果传递到第二个Web服务以获得最终结果

我有两个Web服务,并且使用Alamofire。

  • 第一个Web服务是.get方法,我已经得到了JSON结果。
  • 对于第二个Web服务是.post方法,我需要将第一个Web服务JSON结果传递给参数以获取最终数据列表。

想要实现:将第一个Web服务json数据传递给第二个Web服务(参数)或任何建议以获取最终数据。 请任何人帮助....

第一个Web服务:

Alamofire.request("http://getconstantTableList",method: .get,encoding: encoding,headers: [ "accept":"application/json","Authorization":"Bearer \(token ?? "")"])
.responseJSON { respo in
print(respo)

结果优先Web服务:

{
    "items": [
        {
            "actionType": 101,"version": 1
        },{
            "actionType": 1015,"version": 1
        }

        ]
}

第二个Web服务:

Alamofire.request("http://getconstantTableData",method: .post,parameters: ???  encoding: encoding,"Authorization":"Bearer \(token ?? "")"])
.responseJSON { response in
print(response)
}
gelanxier1989 回答:使用Alamofire Swift将JSON结果传递到第二个Web服务以获得最终结果

您可以通过在第一个api的响应块内调用第二个api来直接传递第一个api的响应。

private func callFirstApi() {
    Alamofire.request("http://GetConstantTableList",method: .get,encoding: encoding,headers: [ "Accept":"application/json","Authorization":"Bearer \(token ?? "")"])
        .responseJSON { response in
            switch response.result {
            case .success(let value):
                if let parameters = value as? [String: Any] {
                    callSecondApi(with: parameters)
                }

            case .failure(let error):
                print(error.localizedDescription)
            }
    }
}

private func callSecondApi(with parameters: [String: Any]) {
    Alamofire.request("http://GetConstantTableData",method: .post,parameters: parameters,"Authorization":"Bearer \(token ?? "")"])
        .responseJSON { response in
            print(response)
    }
}
本文链接:https://www.f2er.com/3073499.html

大家都在问