如何将AFNetworking代码转换为Alamofire?

问题描述 投票:1回答:2

我想将以下代码转换为Alamofire版本,但我对AFHTTPSessionManager部分感到困惑。

+(void)requestWithUrlString:(NSString *)URLstring method:(NSString *)method parameters:(NSMutableDictionary *)parameters success:(void (^)(id))success faiure:(void (^)(NSError *))failure{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager sharedManager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil ];

    manager.requestSerializer.timeoutInterval = 20;

    [manager POST:URLstring parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

感谢您的任何帮助。

ios objective-c alamofire afnetworking
2个回答
0
投票

用这个

Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters as? [String : Any]).responseJSON { response in
                switch response.result
     {
case .success :                         
    case .failure :
    }

0
投票

使用此类用于发送请求

final class ServerManager: SessionManager {

    //----------------------------------------------------------------

   class var sessionConfig : URLSessionConfiguration {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 60 // seconds
        configuration.timeoutIntervalForResource = 60
        return configuration

    }
    class var sessionConfigForDownload : URLSessionConfiguration {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 20 // seconds
        configuration.timeoutIntervalForResource = 20
        return configuration

    }

    static let sharedManager = ServerManager(configuration: sessionConfig, delegate: SessionDelegate.init(), serverTrustPolicyManager: nil)
    static let shareDownloadManager = ServerManager(configuration: sessionConfigForDownload, delegate: SessionDelegate.init(), serverTrustPolicyManager: nil)

    //------------------------------------------------------------

    override init(configuration: URLSessionConfiguration, delegate: SessionDelegate, serverTrustPolicyManager: ServerTrustPolicyManager?) {
        super.init(configuration: configuration, delegate: delegate, serverTrustPolicyManager: serverTrustPolicyManager)
    }

    //------------------------------------------------------------




    func postRequestToServer(_ url : String, parameter : Parameters? = nil, encoding : ParameterEncoding = URLEncoding.default, header : HTTPHeaders? = nil , success: @escaping (Any) -> Void, failure: @escaping (Error) -> Void) -> Void {
            self.request(url, method: .post, parameters: parameter, encoding: encoding, headers: header).responseJSON{ (response) in
                switch response.result {
                case .success:
                    print("Validation Successful")
                    success(response.result.value)
                case .failure(let error):
                    print(error)
                    failure(error)
                }
            }
        }
}

注意:这只是未使用XCode测试的样本。你可以在这里添加像get,put,patch这样的方法,就像我展示的例子一样

希望它对你有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.