服务器请求超时的多次尝试机制 AFNetWorking

http://www.mamicode.com/info-detail-511816.html


+(void)getUserDetailWithUserId:(long)userid withToken:(NSString*)token withSuccess:(SuccessBlock)success withFail:(FailBlock)fail{

    

    NSString * head = [NSString stringWithFormat:@"%@?ticket=",NO_LOGIN_MAIN_WEB_URL];

    NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:@(userid),@"userId", nil];

    NSString * dictjson = [dict jsonString];

    NSString * deviceStr = DEVICEMSG;

    NSString * content = [NSString stringWithFormat:@"%@&_s=user&_m=get_userDetailInfo&type=w&data=%@",deviceStr,dictjson];

    NSString * userstr = [head stringByAppendingFormat:@"%@%@",token,content];

    [self getuserDetaulRequestMaxTimes:3 withUrl:userstr withSuccessBlock:success withFailBlock:fail];

    

}


+(void)getuserDetaulRequestMaxTimes:(NSInteger)times withUrl:(NSString*)urlStr withSuccessBlock:(SuccessBlock)success withFailBlock:(FailBlock)fail{

    if ([DDClientState shareInstance].networkState==DDNetWorkDisconnect) {

        NSError * error = [NSError errorWithDomain:@"很抱歉当前没有网络" code:0 userInfo:nil];

        fail(error);

    }else{

        if (times<=0) {

            NSError * error = [NSError errorWithDomain:@"三次尝试机制,userDetail仍然失败" code:0 userInfo:nil];

            fail(error);

        }else{

            [self getRequestWithUrl:urlStr  withTimes:(NSInteger)times  successBlock:^(id result) {

                if (success) {

                    success(result);

                }

            } failBlock:^(id failResult) {

                [self getuserDetaulRequestMaxTimes:times-1 withUrl:urlStr withSuccessBlock:success withFailBlock:fail];

            }];

        }

    }


}

# pragma mark - AFHTTPSEssion

+(void)getRequestWithUrl:(NSString *)url  withTimes:(NSInteger)times  successBlock:(SuccessBlock)success failBlock:(FailBlock)fail{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.requestSerializer.timeoutInterval = 10*times;

    manager.requestSerializer  = [AFHTTPRequestSerializer serializer];

    [manager.requestSerializer setHTTPShouldHandleCookies:NO];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

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

    NSString  *getURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [manager GET:getURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        if ([dic[@"success"] boolValue]) {

            success(dic);

        }else{

            fail(dic);

        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        fail(error.localizedDescription);

    }];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值