zoukankan      html  css  js  c++  java
  • Alamofire 网络请求封装

    import Foundation
    import Alamofire
    
    typealias SuccessBlock = ([String:Any]) -> Void
    typealias FailureBlock = (AnyObject) -> Void
    typealias ProgressBlock = (Float) -> Void
    
    class LKNetWorkTool: NSObject {
        
        //MARK: - GET请求
        class func GET(url:String,param:[String:Any]?,success: @escaping SuccessBlock) {
                if param != nil {
                    print("
     param:")
                    print(param! as [String:Any])
                }
                print("url===:" + url)
                let urlPath:URL = URL(string: url)!
                let headers:HTTPHeaders = ["Content-Type":"application/json;charset=utf-8"]
                let request = AF.request(urlPath,method: .get,parameters: param,encoding: JSONEncoding.default, headers: headers)
                request.responseJSON { (response) in
                    DispatchQueue.global().async(execute: {
                        print(response.result)
                        switch response.result {
                        case let .success(result):
                            do {
                                let resultDict:[String:Any] = result as! [String:Any]
                                DispatchQueue.main.async(execute: {
                                    /** 返回码处理 */
                                    let resp_code: Int = (resultDict["resp_code"] as! Int)
                                    switch resp_code {
                                    case 0:
                                        success(resultDict)
                                    case 1: break
                                        // SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    default: break
                                        // SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    }
                                })
                            }
                        case let .failure(error):
                            print(error)
                        }
    
                    })
                }
            }
            //MARK: - POST请求  字典参数 ["id":"1","value":""]
            class func POST(url:String,param:[String:Any]?,success: @escaping SuccessBlock) {
                if param != nil {
                    print("
     param:")
                    print(param! as [String:Any])
                }
                print("url===:" + url)
                if url.count == 0 {
                    print("网络请求连接不正确")
                    return;
                }
                let urlPath:URL = URL(string: url)!
                let headers:HTTPHeaders = ["Content-Type":"application/json;charset=UTF-8"]
                let request = AF.request(urlPath,method: .post,parameters: param,encoding: JSONEncoding.default, headers: headers)
                request.responseJSON { (response) in
                    DispatchQueue.global().async(execute: {
                        print(response.result)
                        switch response.result {
                        case let .success(result):
                            do {
                                let resultDict:[String:Any] = result as! [String:Any]
                                DispatchQueue.main.async(execute: {
                                    /** 返回码处理 */
                                    let resp_code: Int = (resultDict["resp_code"] as! Int)
                                    switch resp_code {
                                    case 0:
                                        success(resultDict)
                                    case 1: break
                                        // SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    default: break
                                        // SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    }
                                })
                            }
                        case let .failure(error):
                            print(error)
                        }
    
                    })
    
                }
            }
            //MARK: - POST请求 数组参数(由于有数组参数的需求 )[["id":"1","value":""],["id":"2","value":""]]
            class func POST2(url:String,param:Array<[String:String]>,success: @escaping SuccessBlock) {
                print("url===:" + url)
                let urlPath:URL = URL(string: url)!
                print("
     param:")
                let data = try? JSONSerialization.data(withJSONObject: param, options: [])
                var urlRequest = URLRequest(url: urlPath)
                urlRequest.httpMethod = "POST"
                urlRequest.httpBody = data
                urlRequest.allHTTPHeaderFields = ["application/json":"Accept","application/json;charset=UTF-8":"Content-Type"]
    
                let request = AF.request(urlRequest)
                request.responseJSON { (response) in
                    DispatchQueue.global().async(execute: {
                        print(response.result)
                        switch response.result {
                        case let .success(result):
                            do {
                                let resultDict:[String:Any] = result as! [String:Any]
                                DispatchQueue.main.async(execute: {
                                    /** 返回码 (Int 类型code 会报崩) */
                                    let code = resultDict["resp_code"]
                                    var resp_code = 0
                                    if code is String {
                                        resp_code = Int(code as! String)!
                                    } else if code is Int {
                                        resp_code = code as! Int
                                    }
                                    switch resp_code {
                                    case 0:
                                        success(resultDict)
                                    case 1: break
                                        // SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    default: break
                                        //SVProgressHUD.showError(withStatus: (resultDict["resp_msg"] as! String))
                                    }
                                })
                            }
                        case let .failure(error):
                            print(error)
                        }
    
                    })
    
                }
            }
    
    }
    

      

  • 相关阅读:
    PHPCMS V9 二次开发 —— 入口程序+系统类库与函数库调用+配置文件调用+函数扩展+控制器扩展技巧
    PHPCMS V9 二次开发 —— 二次开发流程
    PHPCMS V9 二次开发 —— 目录结构分析
    如何让pc端网站在手机上可以等比缩放的整个显示
    上海科技馆+博物馆两日游
    旅行的意义
    这样预判牛市第一波能涨到多少点比较靠谱
    历次牛市规律告诉你:如果只有五万元,买什么股票最赚钱?捕捉十年十倍大牛股
    算法竞赛在线测评 competitive programming
    不要做孩子学习的拐杖
  • 原文地址:https://www.cnblogs.com/likun123/p/14437799.html
Copyright © 2011-2022 走看看