swift的AFNetWorking——Alamofire

想了想没什么好写的~大家还是github看文档吧,用法不难,下面就给一个我封装好的管理类,一共两个方法,1.简单的请求,2.上传图片的请求

关于上传图片的请求因为现在Alamofire不支持文件和其他参数一起上传,所以我这里是手动吧参数拼接到请求地址里面了这样就可以实现图片和参数同时上传


swift的AFNetWorking——Alamofire_第1张图片
swift的AFNetWorking——Alamofire_第2张图片

由于不能上传文件,还是那就百度网盘吧

http://pan.baidu.com/s/1gfNOVuV


swift3.0后Alamofire也变成了4.0以前的这个不适用4.0.我改了改直接贴代码吧。。。

```

/*!

通用的基本访问接口

- parameter Url:        请求的地址

- parameter parameters: 参数集合

*/

class func generalNormalPostMethods(_ Url:NSString, requestParameters:NSDictionary, completion: @escaping (NSDictionary?, NSError?) ->Void)->Void{

let url = URL.init(string: Url as String)

var urlRequest = URLRequest(url: url!)

urlRequest.httpMethod = "POST"

do {

urlRequest.httpBody = try JSONSerialization.data(withJSONObject: requestParameters, options: [])

} catch {

// No-op

}

urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

Alamofire.request(urlRequest as URLRequestConvertible)

.validate(statusCode: 200..<300)

.validate(contentType: ["application/json"])

.responseJSON { response in

print(response.request)  // original URL request

print(response.response) // URL response

print(response.data)    // server data

print(response.result)  // result of response serialization

switch response.result {

case .success:

print("Validation Successful")

if let JSON = response.result.value {

print("JSON: \(JSON)")

}

let JSON = response.result.value as?NSDictionary

return completion(JSON,nil)

case .failure(let error):

print(error)

return completion(nil,error as NSError?)

}

}

}

/*!

上传图片接口(主要是头像)

- parameter Url:        地址

- parameter parameters: 图片以外的参数

- parameter imageArr:  图片数组

- parameter completion: 返回的闭包

*/

class func generalUpLoadImagePostMethods(_ Url:NSString, parameters:NSMutableDictionary, imageArr: NSArray, completion: @escaping (NSDictionary?, NSError?) ->Void)->Void{

var theUrl = (Url as String) + "?"

let allKey:NSArray = parameters.allKeys as NSArray

for index in 0 ..< allKey.count{

if index != allKey.count - 1 {

let stringKey:NSString = allKey[index] as! NSString

let stringValue:NSString = parameters.object(forKey: allKey[index])as! NSString

let string = (stringKey as String) + "=" + (stringValue as String) + "&"

theUrl = (theUrl as String) + string

}else{

let stringKey:NSString = allKey[index] as! NSString

let stringValue:NSString = parameters.object(forKey: allKey[index])as! NSString

let string = (stringKey as String) + "=" + (stringValue as String)

theUrl = (theUrl as String) + string

}

}

print(theUrl)

Alamofire.upload(

multipartFormData: { multipartFormData in

for item in imageArr{

multipartFormData.append(UIImagePNGRepresentation(item as! UIImage)!, withName: "headImg",fileName: "headimgad.png", mimeType: "image/png")

}

},

to: theUrl,

encodingCompletion: { encodingResult in

switch encodingResult {

case .success(let upload, _, _):

upload.responseJSON { response in

debugPrint(response)

if let JSON = response.result.value {

print("JSON: \(JSON)")

}

let JSON = response.result.value as?NSDictionary

return completion(JSON,nil)

}

case .failure(let error):

print(error)

let errors = NSError(domain: "网络错误", code: 500, userInfo: nil)

return completion(nil,errors)

}

}

)

}

```

你可能感兴趣的:(swift的AFNetWorking——Alamofire)