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

Sac*_*ram 1 objective-c ios afnetworking

如何使用AFHttpClient设置时间间隔

   AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
Run Code Online (Sandbox Code Playgroud)

ces*_*fry 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
    }];
Run Code Online (Sandbox Code Playgroud)