2

我将 RestKit 用于返回 JSON 数据的简单请求。我已经实现了这个代码,它只在第一次调用 viewDidLoad 时起作用,第二次不执行请求。为什么??

- (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];


    }
4

1 回答 1

0

我也有这个问题,但我找到了解决方案。在 .h 文件中定义 RKClient,如下所示:

@property (retain) RKClient *downloadClient;

然后在您的 .m 文件中合成 downloadClient ,如下所示:

@synthesize downloadClient;

然后在您的 didLoadResponse 方法中,您在完成后释放 downloadClient,如下所示:

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

[downloadClient release];

}
于 2012-08-03T15:01:34.647 回答