2012-08-28 21 views
1

如何使用RestKit进行同步请求?如何在RestKit中创建同步请求?

我用这样的方式早些时候(SBJSON):

UIDevice *myDevice = [UIDevice currentDevice]; 

    NSString *deviceUDID = [myDevice uniqueIdentifier]; 

    double v = [[[UIDevice currentDevice] systemVersion]doubleValue]; 
    NSString *version=[NSString stringWithFormat:@"%@ %.1f",deviceType,v]; 
    NSString *encodedParam1 =[version stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

    NSString *requestString = [NSString stringWithFormat:@"method=views.get&view_name=client_list",nil]; 

    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 

    NSString *urlString = [NSString stringWithFormat:@"http://localhost/index.php?oper=StoreDeviceId&device_id=%@&device_version=%@",deviceUDID,encodedParam1]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]]; 

    [request setHTTPMethod: @"POST"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    [request setHTTPBody: requestData]; 

    //Data returned by WebService 

    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; 

    [request release]; 

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; 

    NSDictionary *dict1 = [returnString JSONValue]; 

相同的操作如何使用restkit框架来处理。

先感谢

+1

mmm如果您确定应用程序在服务器处理请求时会冻结,您确定要这么做吗?你可以在Restkit中使用块,这样你就可以将代码编写为“同步”,但你会得到异步 – clopez

+0

我的答案是否适合你? – clopez

回答

0

要使用RestKit一个同步请求,建立所述RKRequest实例作为正常使用后RKRequest-sendSynchronously方法。

+0

您可以提供该请求的示例代码并响应操作 – Senthilkumar

1

下面是使用RKClient

//Configure RKLog 
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 

//Set Client 
RKClient *client = [RKClient clientWithBaseURLString:@"some_base_url"]; 

//Params to be send 
NSDictionary *queryParameters = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"first_value",@"2",@"second_value",nil]; 

//Prepare the request and send it 
RKRequest *request = [client post:@"path" params:queryParameters delegate:nil]; 
RKResponse *response = [request sendSynchronously]; 

//Process the response 
NSString *stringResponse = [[NSString alloc] initWithData:[response body] encoding: NSUTF8StringEncoding]; 
NSDictionary *dict1 = [stringResponse JSONValue]; 

同步后的一个例子,但我建议使用使用块而不是异步调用!