任何人都可以在post方法中给我AFNetworking 3.0的例子吗?

时间:2017-01-05 03:50:56

标签: swift3 afnetworking-3

我很震惊,因为没有AFHTTPRequestoperation可以找到它的难度。请在swift中使用afnetworking 3.0。

2 个答案:

答案 0 :(得分:1)

在Afnetworking 3.0中删除了AFHTTPRequestoperation类

https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-3.0-Migration-Guide

试试这个:

func PostData(){

    let parameters : NSMutableDictionary? = [

        "UserID": String(300),
        "UserProfileID": String(356)]

        let manager = AFHTTPSessionManager()
        let serializerRequest = AFJSONRequestSerializer()
        serializerRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        manager.requestSerializer = serializerRequest

        let serializerResponse = AFJSONResponseSerializer()
        serializerResponse.readingOptions = JSONSerialization.ReadingOptions.allowFragments
        serializerResponse.acceptableContentTypes = ((((NSSet(object: "application/json") as! Set<String>) as Set<String>) as Set<String>) as Set<String>) as Set<String>;
        manager.responseSerializer = serializerResponse
        manager.post(Webserive.DefaultProfile, parameters: parameters, progress: nil, success: { (task: URLSessionDataTask, responseObject: Any?) in
        if (responseObject as? [String: AnyObject]) != nil {

             print("responseObject \(responseObject)")
        }
        }) { (task: URLSessionDataTask?, error: Error) in
            print("POST fails with error \(error)")
       }
}

答案 1 :(得分:0)

如果您需要在swift上实现它,请使用Alamofire。查看答案here,其中显示了Alamofire post method的示例。