Alamofire: finished with error - code: -1001

旧城冷巷雨未停 提交于 2019-11-26 18:35:12

问题


Code like this:

import Foundation
import Alamofire
struct Request {
var alamoFireManager : SessionManager?
init() {
    let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = 20
    configuration.requestCachePolicy = .useProtocolCachePolicy
            alamoFireManager = Alamofire.SessionManager(configuration: configuration)
}

func sendRequest() {

    alamoFireManager?.request(url,method: method, parameters: body, encoding: JSONEncoding.default,headers: headers).responseJSON { response in

    }
}
}

回答1:


I have done this and it's working (swift 4 Code).

import UIKit
import Alamofire

class SplashViewController: UIViewController {

    var alamoFireManager = Alamofire.SessionManager.default

    override func viewDidLoad() {
        super.viewDidLoad()

        self.callPostApi()

    }

    func callPostApi() {

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 20 // seconds
        configuration.timeoutIntervalForResource = 20
        configuration.requestCachePolicy = .useProtocolCachePolicy
        alamoFireManager = Alamofire.SessionManager(configuration: configuration)

        alamoFireManager.request(url, method: .post, parameters: ["jsondata":base64EncodedString])
            .responseJSON { response in

                switch (response.result) {
                case .success:

                    print(response.data as? Data)

                    if let json = response.result.value {
                        print("JSON: \(json)") // Here is your JSON Response
                    }
                    //do json stuff

                case .failure(let error):

                    if error._code == NSURLErrorTimedOut || error._code == -1005{
                        //HANDLE TIMEOUT HERE
                        print("TimeOut")
                    }
                    print("\n\nAuth request failed with error:\n \(error)")
                    break
               }
        }

    }

}



回答2:


SessionManager not works for me correctly. It's better to using code like this in Alamofire:

let request = URLRequest(url: url)
request.httpMethod = "GET"
//Some configuration


Alamofire.request(request).responseJSON {
                response in

}



回答3:


It's a timeout error. Just increase the value of the timeout, or improve the performance of your API, if possible.



来源:https://stackoverflow.com/questions/51607630/alamofire-finished-with-error-code-1001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!