-
用到三方,安装方法不再赘述。
创建HTTPManager.swift管理类
- 引入头文件
import Alamofire
import SwiftyJSON
- 创建API结构体
struct API {
//任何地方调用 API.hostName + '具体地址',
static let hostName = "https://normal"
//这里可以写其他的,用于测试
static let hostNameTest = "http://test"
}
- 创建管理类单例
class func sharedInstance() -> HTTPManager {
struct Static {
static let instance = HTTPManager()
}
return Static.instance
}
- 数据请求方法封装
//MARK:数据请求方法
///数据请求方法
///- Parameters:
/// - url : 接口地址
/// - type : 请求类型 GET,POST等
/// - params : 参数 [String : AnyObject]
/// - success: 成功的回调
/// - failure: 失败的回调
func exchangeData(url:String,type:HTTPMethod,params:[String : AnyObject]?, success:@escaping ((_ json: JSON)->()),failure:@escaping ((_ json: String)->()) ){
let headers: HTTPHeaders = [
//这里配置headers
]
Alamofire.request(url, method: type, parameters: params, encoding: URLEncoding.methodDependent,headers: headers).responseJSON { (response) in
if(((response.response?.statusCode)! > 199) && ((response.response?.statusCode)! < 300)){
//一般这里就是成功的
if (response.result.value != nil) {
//获取数据部位空就把数据返回
let a = JSON(response.result.value!);
success(a);
}else{
//不然返回空
success([])
}
}else if(response.response?.statusCode == 400){
//出现报错信息
//这些错误代码按需取用
failure("\(String(describing: response.response?.statusCode))");
}else if(response.response?.statusCode == 401){
failure("用户验证信息有误");
}else if(response.response?.statusCode == 404){
failure("接口被外星人抓走了")
}else if(response.response?.statusCode == 406){
failure("接口版本不符合");
}else if(response.response?.statusCode == 429){
failure("请求频率太高");
}else if(response.response?.statusCode == 500){
failure("服务器正在维护");
}else{
failure("服务器正在维护");
}
}
}
- 使用方法
let url = API.hostName + "/api/hospitals";
HTTPManager.sharedInstance().exchangeData(url: url, type: .get, params: nil, success: { (json) in
print(json);
}) { (reason) in
print(reason);
}
- HTTPManager.swift的完整代码如下
import Foundation
import Alamofire
import SwiftyJSON
struct API {
//此处根据本地键值 debug ,用来作为baseURL,一个可以用来做测试的接口,一个定义为线上接口.可以设置一个按钮,管理debug字段,重启app就可以更换接口
static let hostName = "https://dev.api.healthydoc.com.cn/"
}
class HTTPManager: NSObject {
class func sharedInstance() -> HTTPManager {
struct Static {
static let instance = HTTPManager()
}
return Static.instance
}
//MARK:数据请求方法
///数据请求方法
///- Parameters:
/// - url : 接口地址
/// - type : 请求类型 GET,POST等
/// - params : 参数 [String : AnyObject]
/// - success: 成功的回调
/// - failure: 失败的回调
func exchangeData(url:String,type:HTTPMethod,params:[String : AnyObject]?, success:@escaping ((_ json: JSON)->()),failure:@escaping ((_ json: String)->()) ){
let accept = "application/json;" + version
let headers: HTTPHeaders = [
//......
]
Alamofire.request(url, method: type, parameters: params, encoding: URLEncoding.methodDependent,headers: headers).responseJSON { (response) in
if(((response.response?.statusCode)! > 199) && ((response.response?.statusCode)! < 300)){
if (response.result.value != nil) {
let a = JSON(response.result.value!);
success(a);
}else{
success([])
}
}else if(response.response?.statusCode == 400){
failure("\(String(describing: response.response?.statusCode))");
}else if(response.response?.statusCode == 401){
failure("用户验证信息有误");
}else if(response.response?.statusCode == 404){
failure("接口被外星人抓走了")
}else if(response.response?.statusCode == 406){
failure("接口版本不符合");
}else if(response.response?.statusCode == 429){
failure("请求频率太高");
}else if(response.response?.statusCode == 500){
failure("服务器正在维护");
}else{
failure("服务器正在维护");
}
}
}
}