2012-06-28 161 views
2

我正在將RestKit用於返回JSON數據的簡單請求。 我已經實現了這個代碼,它只在第一次調用viewDidLoad時,第二次不執行請求。爲什麼??RestKit iOS - 第二次失敗

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self planOtpTrip]; 
} 

-(void) planOtpTrip{ 

     NSLog(@"[INFO] planoOtpTrip"); 

     client = [RKClient clientWithBaseURLString:@"http://myserver/opentripplanner-api-webapp/ws"]; 

     [client setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

     NSDictionary *queryParameters = [NSDictionary dictionaryWithObjectsAndKeys:@"45.028952,7.624598",@"fromPlace",@"45.06123,7.65981", @"toPlace",@"TRANSIT,WALK" ,@"mode", nil]; 



     // Imposto il nome della API da richiamare 
     NSString *getResourcePath = RKPathAppendQueryParams(@"/plan", queryParameters); 

     [client get:getResourcePath delegate:self]; 

    } 

    - (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response { 
     // where you handle response object 

     NSLog(@"Risposta ricevuta: %@", response.URL); 

     NSLog(@"Response Body: %@", response.bodyAsString); 

     id jsonParser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON]; 
     NSError *error = nil; 
     NSDictionary* parsedResponse = [jsonParser objectFromString:[response bodyAsString] error:&error]; 
     if (error == nil) 
     { 
      NSLog(@"GET returned with HTTP Code %d and parsedContent: %@", [response statusCode], parsedResponse); 

     } 
     else 
     { 
      NSLog(@"Error: %@", error); 
     } 


     return [self renderResponse:parsedResponse]; 


    } 

回答

0

我也有這個問題,但我找到了解決方案。定義RKClient在.h文件中像這樣:

@property (retain) RKClient *downloadClient; 
在.m文件

然後合成downloadClient像這樣:

@synthesize downloadClient; 

,然後在didLoadResponse方法你釋放downloadClient當你做起來難像這樣:

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response 
{ 
//do something with your response 

[downloadClient release]; 

}