在Almofire中使用标头,HTTP方法,参数和对被调用类的回调来调用api

时间:2018-09-04 12:25:02

标签: swift alamofire xcode9.3

在函数下方,我们基于此传递了url,参数,方法和消息,我们必须调用api并将回调返回给被调用函数。

这是网址:

http://192.168.0.204/Test/api/DashboardIncomeAndHeadCode/FunGetIncomeOrMenuHeadDetails?StrLocDate=&StrLocRestaurantIds=604808&IntLocCustomerid=6557&StrLocFlag=A&StrLocGroupId=524&StrLocIsIncomeOrMenuHeadCode=I&StrLocFromToDate=

注意:关键标题也使用您的URL,而Params使用您的URL,这是供参考

func sendRequestDetailed(uri: String, isShowLoadingView:Bool, params: JSONDictionary?,method: HTTPMethod,message: JSONDictionary?, completionHandler: @escaping (SDNetworkResponse) -> ()) {
    if isShowLoadingView == true {
        DCAppDelegate.showLoadingView()
    }

    let url = urlWithParams(uri: uri, params:params)
    let request = NSMutableURLRequest(url:url as URL)
    request.httpMethod = method.rawValue
    request.timeoutInterval = 120;
    if debugHttp { NSLog("url: \(request.httpMethod) \(url)") }

    if (message != nil) {
        let messageData = try? JSONSerialization.data(withJSONObject: message!, options:[])
        let messageString = NSString(data: messageData!, encoding: String.Encoding.utf8.rawValue)

        request.httpBody = messageData
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        if debugHttp { NSLog("message: \(String(describing: messageString))") }
    }
    request.setValue("KeyValue", forHTTPHeaderField:"Key")
    let headers: HTTPHeaders = [
        "Key": "Value",
    ]

    if (method == .POST) {
        Alamofire.request(uri, method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
            switch response.result{
            case .success(let value):
                let SDNetworkResponseValue =
                    self.prepareResponseObject(success: response.result.isSuccess, withData: value as Any, andError: response.result.error as NSError? ?? NSError.init(domain: "successfull", code: 1, userInfo: [NSLocalizedDescriptionKey:"request successfull"]))
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue )
            case .failure(let error):
                let SDNetworkResponseValue = self.prepareResponseObject(success: response.result.isSuccess, withData: response.result.value as Any , andError: (error as NSError))
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            }
        }
    }else if(method == .GET){
        Alamofire.request(uri, method: .get, parameters: params, encoding: JSONEncoding.default,headers:headers).responseJSON { response in
            switch response.result {
            case .success(let value):
                let SDNetworkResponseValue =
                    self.prepareResponseObject(success: response.result.isSuccess, withData: value as Any, andError: response.result.error as NSError? ?? NSError.init(domain: "Error", code: 200, userInfo: [NSLocalizedDescriptionKey:"Something went wrong"]))
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            case .failure(let error):
                let SDNetworkResponseValue = self.prepareResponseObject(success: response.result.isSuccess, withData: response.result.value as AnyObject , andError: error as NSError)
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            }
        }
    }else if(method == .PUT){
        Alamofire.request(uri, method: .put, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
            switch response.result {
            case .success(let value):
                let SDNetworkResponseValue =
                    self.prepareResponseObject(success: response.result.isSuccess, withData: value as Any, andError: response.result.error as NSError? ?? NSError.init(domain: "Error", code: 200, userInfo: [NSLocalizedDescriptionKey:"Something went wrong"]))
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            case .failure(let error):
                let SDNetworkResponseValue = self.prepareResponseObject(success: response.result.isSuccess, withData: response.result.value as AnyObject , andError: error as NSError)
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            }
        }
    }else if(method == .DELETE){
        Alamofire.request(uri, method: .delete, parameters: params, encoding: JSONEncoding.default).responseJSON { response in
            switch response.result {
            case .success(let value):
                let SDNetworkResponseValue =
                    self.prepareResponseObject(success: response.result.isSuccess, withData: value as Any, andError: response.result.error as NSError? ?? NSError.init(domain: "Error", code: 200, userInfo: [NSLocalizedDescriptionKey:"Something went wrong"]))
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            case .failure(let error):
                let SDNetworkResponseValue = self.prepareResponseObject(success: response.result.isSuccess, withData: response.result.value as AnyObject , andError: error as NSError)
                DCAppDelegate.removeLoadingView()
                completionHandler(SDNetworkResponseValue)
            }
        }
    }
}

但是此函数总是返回错误:

  

▿失败:错误域= NSURLErrorDomain代码= -1001“请求超时。” UserInfo = {NSErrorFailingURLStringKey = http://192.168.0.204/Test/api/DashboardIncomeAndHeadCode/FunGetIncomeOrMenuHeadDetails,_kCFStreamErrorCodeKey = -2102,NSErrorFailingURLKey = http://192.168.0.204/Test/api/DashboardIncomeAndHeadCode/FunGetIncomeOrMenuHeadDetails,NSLocalizedDescription =请求超时。,_kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x14da7c50 {Error Domain = kCFErrorDomainCFNetwork Code = -100 null)“ UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -2102}}}

0 个答案:

没有答案