Swift|IOS15之swift的Alamofire 5.4框架的网络封装
IOS15之swift的Alamofire 5.4框架的网络封装 此Alamofire 版本较高,是基于IOS15 和最新的 Alamofire 5.4.4 版本,截止我发稿的时候,最新版本,网上其他框架的版本较低,api早就淘汰了,或者其他写法,(对回调函数所使用到的关键字@escaping)要重点掌握
后台我采用Java 的springBoot进行封装测试的。
涉及swift基础语法,cocoaPods的使用,swift的闭包,回调,swift5.5语法新特性,单例模式的使用等,类方法的定义,Alamofire 的get,post,put ,delete请求,带参数,或者不带参数,都要拿捏的非常准备到位
文章图片
文章图片
//
//NetworkTools.swift
//Alamofire1
//
//Created by lujun on 2021/10/2.
//import UIKit
import Alamofireenum MethodType {case GET
case POST
}class NetworkTools{//类方法
class func requestData(type: MethodType,urlString: String, parameters: [String : Any]? = nil,finishedCallback : @escaping ( _ results : Any) -> ()){//let type1 = type==MethodType.GET ? ".get" : ".post"
if(type==MethodType.GET){Alamofire.AF.request(urlString).responseJSON {
(response) in
switch response.result {case .success(let json):
finishedCallback(json)
break
case .failure(let error):
print("error:\(error)")
break
}
}
}
if(type==MethodType.POST){Alamofire.AF.request(urlString, method: .post, parameters: parameters).responseJSON {
(response) in
switch response.result {case .success(let json):
finishedCallback(json)
break
case .failure(let error):
print("error:\(error)")
break
}
}
}
}
//类方法
class func requestDataWithParam(type: MethodType,urlString: String, parameters: [String : Any],finishedCallback : @escaping ( _ results : Any) -> ()){AF.request(urlString, parameters: parameters).responseJSON {
(response) in
switch response.result {case .success(let json):
finishedCallback(json)
break
case .failure(let error):
print("error:\(error)")
break
}
}
}
}
测试代码如下:
//
//ViewController.swift
//Alamofire1
//
//Created by lujun on 2021/10/2.
//import UIKit
import Alamofireclass ViewController: UIViewController {/*
AF.request("https://httpbin.org/get").responseJSON{ (response) in
guard let result = response.value else{
print(response.error ?? "")
return
}
print(result)
}
*/
override func viewDidLoad() {super.viewDidLoad()
NetworkTools.requestDataWithParam(type: .GET, urlString: "http://localhost:8081/demo3",parameters: ["sn":"lujun"]) {
result in
print(result)
}
}
func getRequest(){let url = "https://httpbin.org/get"
Alamofire.AF.request(url).responseJSON {
(response) in
switch response.result {case .success(let json):
print(json)
// Success in request and do a async or sync(NOT UI) task here.
break
case .failure(let error):
print("error:\(error)")
break
}
}
}
func postRequest(){let url = "https://httpbin.org/post"
Alamofire.AF.request(url, method: .post, parameters: ["name":"jack","password":"123456"]).responseJSON {
(response) in
switch response.result {case .success(let json):
print(json)
break
case .failure(let error):
print("error:\(error)")
break
}
}
}}
Java 后台
package com.example.demo.controller;
import com.example.demo.dao.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class DemoController {@GetMapping(value = "https://www.it610.com/demo")
public String test2(String sn){System.out.println(sn);
return "test";
}
@GetMapping(value = "https://www.it610.com/demo3")
public Object test23(String sn){Map m1 =new HashMap();
System.out.println(sn);
User user = new User("lujun", 18, "123@qq.com");
m1.put("u1",user);
return m1;
}
}
【Swift|IOS15之swift的Alamofire 5.4框架的网络封装】项目文件名在我的资源文件下。名称是Alamofire1.zip
推荐阅读
- PMSJ寻平面设计师之现代(Hyundai)
- 太平之莲
- 闲杂“细雨”
- 七年之痒之后
- 深入理解Go之generate
- 由浅入深理解AOP
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 生活随笔|好天气下的意外之喜
- 感恩之旅第75天
- python学习之|python学习之 实现QQ自动发送消息