【问题标题】:API call using Alamofire sometimes returning connection fail error使用 Alamofire 的 API 调用有时会返回连接失败错误
【发布时间】:2018-09-15 02:32:56
【问题描述】:

我正在使用 alamofire 进行 api 调用。请求调用每次都会成功阻塞,但有时对于相同的请求,调用会失败阻塞并出现错误“网络连接丢失”。由于网络连接没有问题,我无法找到特定问题的解决方案。这是我的代码-

let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 120
        manager.request(url, method: .post, parameters: requestDictionary ?? [:], encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) in
            switch response.result {

            case .success:
                KVNProgress.dismiss()
                let jsonString: String = String(data: response.data!, encoding: String.Encoding.utf8)!
                print(jsonString)
                let dict : [String : Any ] = response.result.value! as! [String : Any]
                let msg = dict["Message"] as? String
                if msg == "Session id is not valid." {

                    //logout process…   

                    }, onCancelClick: {

                    })

                }else{
                    success(response.result.value! as AnyObject)
                }

            case .failure(let error):
                KVNProgress.dismiss()
                print("Request failed with error: \(error.localizedDescription)")
                failure(“Unexpected error”)
            }
        })

提前致谢。 :)

我的代码在我已经检查过互联网连接的块内-

 if(Utilities.checkInternetConnection())
   {
        // My previous code
    }

    else
    {
        connectionFailed(Constant.serverAPI.errorMessages.kNoInternetConnectionMessage)
    }

【问题讨论】:

  • 在 Postman 中检查所有 API
  • 我检查过了。它工作正常。

标签: ios swift api alamofire


【解决方案1】:

我也有这个问题,多次API调用返回连接失败错误。

我通过捕获连接失败错误并从连接失败关闭中再次调用 API 错误来解决它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 2014-06-22
    • 1970-01-01
    • 2015-01-18
    • 2022-01-10
    • 2021-06-30
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多