处理背靠背的HTTP帖子SWIFT 4.0

处理背靠背的HTTP帖子SWIFT 4.0

问题描述:

我目前正试图从服务器获取一些数据。为此,我必须创建一个JSON(req1)对象,并将其作为请求发送给第一个API。然后,API返回给我一个JSON(res1)。从该JSON(res1)中,我必须提取一些数据(非常冗长,我们称之为Data1)并创建另一个JSON对象(req2),并将其作为请求打到另一个API并等待响应(res2)。处理背靠背的HTTP帖子SWIFT 4.0

现在,它是一个背靠背功能。当我调用第一个API时,需要很长时间才能做出响应,到那个时候,我的第二个API会被调用。因此,我从服务器收到一个错误。我该如何过来呢?

ALGO:

  1. 呼叫API_A()
  2. 收到JSON_A
  3. 提取JSON_A 4.Call API_B(JASON_A.someparts)
  4. 收到JSON_B
  5. 提取JSON_B

以下是代码:

let json1 = JSON() 
 
let json2 = JSON() //my final result 
 

 
override func viewDidLoad() { 
 
     super.viewDidLoad() 
 
\t  firstApiHit {() ->() in 
 
      secondApiHit(jsonV: json1) 
 
     } 
 
    } 
 

 
func firstApiHit(handleComplete:(()->())){ 
 
\t let url1 = URL(string : firstApiURL) 
 
\t let json: [String: String] = ["Data1:Data1Rest"] 
 
\t let jsonData = try? JSONSerialization.data(withJSONObject: json) 
 
\t json1 = httpPost(jsonData: jsonData!, url: url1!) //hit to the first API! 
 
\t handleComplete() 
 
} 
 

 
func secondApiHit(jsonV : JSON){ 
 
\t let url1 = URL(string : secondApiURL) 
 
\t var value = String() 
 
\t extractedValue = String(describing:jsonV["DataX"]) 
 
\t var json = [String: String]() 
 
\t json = ["Data1":extractedValue] 
 
\t let jsonData = try? JSONSerialization.data(withJSONObject: json) 
 
    let json2 = httpPost(jsonData: jsonData!, url: url2!) // my final result! 
 
\t \t //Need the json2 as the final Result!! 
 
} 
 
    
 
func httpPost(jsonData: Data, url: URL) -> JSON { 
 
\t if !jsonData.isEmpty { 
 
\t \t var request = URLRequest(url: url) 
 
\t \t request.httpMethod = "POST" 
 
\t \t request.httpBody = jsonData 
 
\t \t request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
 
\t \t URLSession.shared.getAllTasks { (openTasks: [URLSessionTask]) in 
 
\t \t \t \t \t \t \t \t \t print("open tasks: \(openTasks)") 
 
\t \t \t \t \t \t \t \t \t } 
 
\t \t let task = URLSession.shared.dataTask(with: request) { data, response, error in 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t guard let data = data, error == nil else { 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t // check for fundamental networking error 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t print("error=\(String(describing: error))") 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t return 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t } 
 
    \t \t  \t if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {   
 
\t \t \t \t \t // check for http errors 
 
\t \t \t \t print("statusCode should be 200, but is \(httpStatus.statusCode)") 
 
\t \t \t \t print("response = \(String(describing: response))") 
 
\t \t \t } 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t 
 
\t \t \t let jsonX = JSON(data: data) //extracting the data! 
 
\t \t } \t 
 
    \t task.resume() 
 
\t } 
 
    return jsonM 
 
}
URLSession.shared.dataTask(...)

+2

“因此,我得到服务器的错误” - 为什么“因此”?什么原因?什么样的错误? – Raphael

+0

你可能想看看Alamofire。 – Raphael

+0

错误是,我无法从第一个响应中提取整个JSON,因此,我的第二个请求不完整,导致第二个响应失败!失败,因为我得到400代码,而不是200,所以我的JSON回来为空。 – Stopshe

httpPost(...)回报结束,因为后者是异步运行。

firstApiHit(...)中,您不应该拨打handleComplete(),而是将其传递到httpPost(...)。您还需要传递JSON作为参数。

override func viewDidLoad() { 
    super.viewDidLoad() 
    firstApiHit { json in 
     secondApiHit(jsonV: json) 
    } 
} 

func firstApiHit(handleComplete: JSON -> Void){ 
    ... 
    json1 = httpPost(jsonData: jsonData!, url: url1!, handleComplete) 
} 

func httpPost(jsonData: Data, url: URL, responseHandler: (JSON) -> Void) { 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     ...         
     let jsonX = JSON(data: data) 
     responseHandler(jsonX) 
    } 
} 

肯定需要学习如何使用关闭和,更具体地说,响应处理模式。否则,你会一次又一次地陷入痛苦的世界。