5

私はうまく機能する次のコードを持っていますが、それをもう少し制御する必要があり、特に0.9で到達可能性コードの使用を開始する必要があります。

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を使用するように変換する方法を見つけるのに苦労しています。

4

1 に答える 1

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]); 
      }];
于 2012-02-27T11:00:15.400 に答える