使用Alamofire呼叫Api时出错

时间:2018-04-22 15:41:34

标签: ios json alamofire swift4 swifty-json

我使用Alamofire及其发布请求来呼叫api。错误并不总是来。只有在很长一段时间后调用api时才会出现。在该错误状态之后,相同的api完美地运行。

enter image description here

这是我正在使用的代码

func getAuthTokenFromServer(completion: @escaping (_ success: Bool) -> Void)
{
    self.showActivityIndicator()

    let deviceUDID      =   UIDevice.current.identifierForVendor!.uuidString

    let deviceModel     =   UIDevice.current.model.description

    let deviceOS        =   UIDevice.current.systemVersion

    let deviceName      =   UIDevice.current.name

    let webURL: String  =   "<SOME URL>"

    let params          =   ["deviceId": deviceUDID, "deviceType": deviceModel, "deviceOS": deviceOS, "deviceName": deviceName] as [String : Any]

    debugPrint(params)

    Alamofire.request(webURL, method: .post , parameters: params,encoding: JSONEncoding.default, headers: nil).responseJSON {
        response in
        switch response.result
        {
        case .success:

            debugPrint(response)

            if let json = response.result.value
            {
                var jsonObj                 = JSON(json)

                print(jsonObj)

                let responseCode       = (jsonObj["response"]["code"]).stringValue

                print(responseCode)

                if (responseCode == "1000")
                {
                    self.firstTextView.text = "\(jsonObj)"
                    completion(true)
                    self.hideActivityIndicator()
                }
                else
                {
                    SetDefaultWrappers().showAlert(info: (jsonObj["response"]["message"]).stringValue, viewController: self)
                    completion(true)
                    self.hideActivityIndicator()
                }
            }
            break
        case .failure(let error):
            print(error)
            debugPrint(response)
            self.hideActivityIndicator()
            completion(true)
            SetDefaultWrappers().showAlert(info: error.localizedDescription, viewController: self) // The alert in the question is called from here
        }
    }
}

有人可以帮助我解决这个问题。我正在使用Alamofire来调用api和SwiftJSON来解析数据。

0 个答案:

没有答案