Alamofire是由原AFNetworking团队研发的适用swift的网络请求框架。
struct ResponseType: Codable {
let success: String
let message: String
let data: [String]
}
public class func request(url:String,
modelType:HTTPMethod = .post,
parameters:[String : String] = [:],
successBlock:@escaping (_ responseData:[String : Any]) -> Void){
let header:HTTPHeaders = ["Accept-Language":"zh-TW","token":""]
// JSONParameterEncoder.default -- 相当于AFN里面的[AFJSONRequestSerializer serializer]
// URLEncodedFormParameterEncoder.default-- 相当于AFN里面的AFPropertyListRequestSerializer
AF.request(url, method: modelType, parameters: parameters, encoder: URLEncodedFormParameterEncoder.default, headers: header, interceptor: nil, requestModifier: nil).responseDecodable(of: ResponseType.self) { response in
if let data = response.data {
let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
print("-*-*-*-*-*-:",String(data: data, encoding: .utf8)!)
successBlock(dict as! Dictionary)
}
}
}