2012-02-23 10 views
5

私は以下のコードをうまく使用していますが、これ以上の制御が必要で、特に0.9のReachabilityコードの使用を開始する必要があります。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]; 
} 

私は「setReachabilityStatusChangeBlock」を利用できるように、私はAFHTTPClientの使用に渡って、この同じコードを変換することができますどのように動作するように苦労しています。

答えて

12

ちょうど

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

シングルトンでAFHTTPClientのサブクラスを作成してある、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]); 
     }]; 
を呼び出します
関連する問題