zoukankan      html  css  js  c++  java
  • 设置AFNetworking网络请求的超时时间

    设置AFNetworking网络请求的超时时间

    也许大家使用的时候已经察觉到,设置AFNetworking的超时时间并不管用,但可以用特殊的方式来处理。

    以下是笔者基于AFNetworking2.5.0封装的GET,POST请求用方法。

    POST请求

    + (AFHTTPRequestOperation *)GETMethod:(NSString *)URLString
                               parameters:(id)parameters
                                  success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                  failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
        
        AFHTTPRequestOperationManager *manager    = [AFHTTPRequestOperationManager manager];
    
        // 设置超时时间
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 10.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
        AFHTTPRequestOperation *httpOperation = [manager GET:URLString
                                                  parameters:parameters
                                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                         if (success) {
                                                             success(operation, responseObject);
                                                         }
                                                     }
                                                     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                         if (failure) {
                                                             failure(operation, error);
                                                         }
                                                     }];
        
        return httpOperation;
    }

    GET请求

    + (AFHTTPRequestOperation *)POSTMethod:(NSString *)URLString
                                parameters:(id)parameters
                                   success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                                   failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
        
        AFHTTPRequestOperationManager *manager            = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer                         = [AFJSONRequestSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        
        
        // 设置超时时间
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 10.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
        
        AFHTTPRequestOperation *httpOperation = [manager POST:URLString
                                                   parameters:parameters
                                                      success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                          if (success) {
                                                              success(operation, responseObject);
                                                          }
                                                      }
                                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                          if (failure) {
                                                              failure(operation, error);
                                                          }
                                                      }];
        
        return httpOperation;
    }

    其中,设置这么一句话即可:

  • 相关阅读:
    内置系统账户:Local system/Network service/Local Service 区别
    python-基于文件导入的特性的单例模式
    php原因 nginx报错[error] 10773#0: *272212065 recv() failed (104: Connection reset by peer) while reading response header from upstream
    实用Django ORM实用操作方法
    session是什么和cookie的区别?
    Python可迭代对象,迭代器,生成器
    浅析python中的GIL锁和协程
    git 常用
    testlink安装
    redmine搭建
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4331033.html
Copyright © 2011-2022 走看看