AFNetworking (AFJSONRequestOperation) convert to AFHTTPClient

前端 未结 1 795
花落未央
花落未央 2020-12-29 16:26

I have the following code that works well but I need a little more control over it and especially need to start using the Reachability code in 0.9.

NSString          


        
相关标签:
1条回答
  • 2020-12-29 16:37

    Just create a subclass of AFHTTPClient with a singleton

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

    And then call the getPath method

    [[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]); 
          }];
    
    0 讨论(0)
提交回复
热议问题