http网络服务-swift-Alamofire

依赖库的github地址

http网络库
Alamofire

解json
SwiftyJSON

Swift Package Manager集成依赖库

swift 复制代码
https://github.com/Alamofire/Alamofire.git

https://github.com/emqx/CocoaMQTT.git

使用

swift 复制代码
import Alamofire
import SwiftyJSON

var http = "http://xxxxxxxx"
var post = "xxx/xxx"
var url: String {http + post}

func action() {
	// 请求参数
    let a: [String: String] = [
        "name": "anna",
        "gender": "girl"
    ]
    
	// 改操作限时3s(request中的参数可以根据需求进行更改)
    AF.request(url, method: .post, parameters: a, encoder: JSONParameterEncoder.default, requestModifier: { $0.timeoutInterval = 3 }).response { response in
        switch response.result {
        case .success(let data):
            if let data = data, let responseString = String(data: data, encoding: .utf8) {
                let jsonData = responseString.data(using: .utf8)
                // 解json
                let json = JSON(jsonData)
                // 假设json里面有一个code元素
                let code = json["code"].intValue
                // 假设json里面有一个数组a,数组a中有一元素b
                let c = json["a"]["b"].stringValue

                // 打印整个json
                print(json)
            }
            
            
        case .failure(let error):
        	// 超时或失败,打印错误
            print("Error: \(error)")
        }
    }
}
相关推荐
李昊哲小课7 分钟前
spring 中 HttpStatus 与 ResponseEntity
spring boot·后端·spring·http·spring cloud·restful
编程爱好者熊浪9 分钟前
测试需要使用HTTPS,怎么申请一个免费的测试域名
网络协议·http·https
limuyang241 分钟前
【http3/quic】cronet 已经原生集成在Android内啦!还不快来开开眼!
android·http·google
HarderCoder1 小时前
Swift 方法全解:实例方法、mutating 方法与类型方法一本通
swift
helloworld工程师1 小时前
Dubbo应用开发之RPC直连开发
网络协议·rpc·dubbo
吐泡泡_2 小时前
TCP(滑动窗口/拥塞窗口补充)
网络协议
HarderCoder2 小时前
Swift 类型转换实用指北:从 is / as 到 Any/AnyObject 的完整路线
swift
HarderCoder2 小时前
Swift 嵌套类型:在复杂类型内部优雅地组织枚举、结构体与协议
swift
游戏开发爱好者88 小时前
HTTPS 内容抓取实战 能抓到什么、怎么抓、不可解密时如何定位(面向开发与 iOS 真机排查)
android·网络协议·ios·小程序·https·uni-app·iphone
Fms_Sa12 小时前
UDP的一次双向通信
网络·网络协议·udp