如何在AFNetworking中使用AFHTTPClient设置超时间隔?

时间:2014-03-26 15:59:26

标签: ios objective-c afnetworking

如何使用AFHttpClient设置时间间隔

   AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

1 个答案:

答案 0 :(得分:5)

超时信息是您需要创建然后提供给客户的NSURLRequest的一部分:

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];


    NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@""] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10]; // <- 10 is the timeout interval

    AFHTTPRequestOperation *op = [httpClient HTTPRequestOperationWithRequest:req success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // DONE
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // FAILED
    }];