0

RestKit 不发送第二个请求我有一个简单的应用程序,有 1 个视图和 2 个按钮,当我第一次单击任何按钮时,按钮的标题是主机名(yandex.ru 和 localhost:3000),但如果我第二次单击任何它不发送它....我做错了什么?

下面是我的 ViewController 代码的一部分

- (IBAction)testRestkit:(UIButton *)sender
{
   RKClient *client =[RKClient clientWithBaseURLString:[NSString stringWithFormat:@"http://%@",sender.titleLabel.text]];
   client.cachePolicy = RKRequestCachePolicyNone;
   [client get:nil delegate:self];
}

- (void)requestWillPrepareForSend:(RKRequest *)request
 {
   NSLog(@"Preparing for request......");
 }

- (void)request:(RKRequest *)request didFailLoadWithError:(NSError *)error
{
   NSLog(@"%@",[error localizedDescription]);
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
{
   if ([response isHTML]) {
     NSLog(@"Loaded html!");
   } else{
     NSLog(@"Loaded some response!"); 
   }
}
4

2 回答 2

0

我认为问题在于您第一次调用clientWithBaseURLString:时,[RKClient sharedClient]单例被初始化(并正确保留),因此后续调用确实有效。当您第二次调用代码时,便利构造函数返回一个自动释放的 RKClient 实例以及另一个基本 url。

在 RestKit 中处理多个基本 URL 的正确方法是管理两个单独的 RKClient 实例,例如

@property (nonatomic, retain) RKClient *localhostClient;
@property (nonatomic, retain) RKClient *remoteClient;

并初始化它们,例如。在您的 init 方法中(或在您的方法中懒惰地)。

static NSString *kRemoteServerUrl = @"http://yandex.ru";
- (IBAction)testRestkit:(UIButton *)sender
{
   RKClient *client = nil;
   if ([sender.titleLabel.text isEqualToString:kRemoteServerUrl]) {
       if (!self.remoteClient) { 
          self.remoteClient = [RKClient clientWithBaseURLString:kRemoteServerUrl];
          client.cachePolicy = RKRequestCachePolicyNone;
       }
       [self.remoteClient get:nil delegate:self];
   } else if (.....)
....
}
于 2012-09-05T09:49:25.417 回答
0

if i send request like code below . they works!!!! someone could explain difference between both methods???

- (IBAction)testRestkit:(UIButton *)sender 
{    
   NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",sender.titleLabel.text]]; 
   RKRequest *theRequest = [[RKRequest alloc] initWithURL:url];
   theRequest.queue = [RKClient sharedClient].requestQueue;
   theRequest.delegate = self;
   [theRequest setMethod:RKRequestMethodGET];
   [theRequest send];
}
于 2012-09-04T17:07:31.570 回答