2012-08-28 49 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]; 

同步後的一個例子,但我建議使用使用塊而不是異步調用!