2012-02-23 17 views
5

में कनवर्ट करता है मेरे पास निम्न कोड है जो अच्छी तरह से काम करता है लेकिन मुझे इसके बारे में थोड़ा अधिक नियंत्रण चाहिए और विशेष रूप से 0.9 में रीचैबिलिटी कोड का उपयोग शुरू करने की आवश्यकता है।AFNetworking (AFJSONRequestOperation) 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]; 
} 

मैं बाहर काम करने के लिए कैसे मैं एक AFHTTPClient उपयोग करने के लिए भर में यह एक ही कोड में बदल सकते हैं, ताकि मैं "setReachabilityStatusChangeBlock" का लाभ ले सकते संघर्ष कर रहा हूँ।

उत्तर

12

बस एक सिंगलटन साथ AFHTTPClient का एक उपवर्ग बनाने

+ (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; 
} 

और फिर 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]); 
     }]; 
फोन
संबंधित मुद्दे