2012-02-23 8 views
5

tengo el siguiente código que funciona bien pero necesito un poco más de control sobre ella y sobre todo necesito para empezar a utilizar el código de accesibilidad en 0,9.AFNetworking (AFJSONRequestOperation) convertir a AFHTTPClient

NSString *urlString = [NSString stringWithFormat:@"http://example.com/API/api.php"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    _self.mainDictionary = [JSON valueForKeyPath:@"elements"]; 
    [_self parseLiveData]; 
} failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON){ 
    //NSLog(@"Failed: %@",[error localizedDescription]);   
}]; 

if (operation !=nil && ([self.sharedQueue operationCount] == 0)) { 
    [self.sharedQueue addOperation:operation]; 
} 

estoy luchando para encontrar la manera que puedo convertir este mismo código a través de utilizar un AFHTTPClient para que pueda tomar ventaja de la "setReachabilityStatusChangeBlock".

Respuesta

12

Basta con crear una subclase de AFHTTPClient con un producto único

+ (id)sharedHTTPClient 
{ 
    static dispatch_once_t pred = 0; 
    __strong static id __httpClient = nil; 
    dispatch_once(&pred, ^{ 
     __httpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/API"]]; 
     [__httpClient setParameterEncoding:AFJSONParameterEncoding]; 
     [__httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    }); 
    return __httpClient; 
} 

y luego llamar al método getPath

[[YourHTTPClient sharedHTTPClient] 
    getPath:@"api.php" 
    parameters:nil 
     success:^(AFHTTPRequestOperation *operation, id JSON){ 
       _self.mainDictionary = [JSON valueForKeyPath:@"elements"]; 
       [_self parseLiveData]; 
     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      //NSLog(@"Failed: %@",[error localizedDescription]); 
     }]; 
Cuestiones relacionadas