2013-01-07 39 views
0

我正在使用restkit構建一個應用程序來與我的web服務進行通信。首先,我對此很陌生。我需要做的是發佈一個用戶名和密碼到我的web服務,使url看起來像這樣。如何使用restkit進行發佈

http://urllinkcompany.com/en/webservice/company-user/login/apikey/[email protected]&pwd=testtest 

當我發佈這個,我得到一個json回來。這個json包含一個狀態碼。 status = 200 --> OK Status = 404 --> NOT ok

現在我試着發佈一些東西和NSLog這個狀態碼。

- (IBAction)login:(id)sender { 

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Person class]]; 
    [mapping addAttributeMappingsFromDictionary:@{ 
    @"data.user.cu_email":  @"cu_email", 

    }]; 


    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:nil]; 

    NSString *baseUrl = @"http://urllinkcompany.com/en/webservice/company-user/login/apikey/key12345678?"; 
    NSString *strUrl = [NSString stringWithFormat:@"%@email=%@&pwd=%@",baseUrl,_txtLogin.text,_txtPass.text]; 
    NSURL *url = [NSURL URLWithString:strUrl]; 
    NSLog(@"url is: %@",strUrl); 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]]; 
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) { 
     NSLog(@"data result is %@", [result valueForKeyPath:@"data.status"]); 
    } failure:nil]; 

} 

但我得到這個錯誤。

E restkit.network:RKObjectRequestOperation.m:285 Object request failed: Underlying HTTP request operation failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x1ed57c50 {NSUnderlyingError=0x20161e70 "bad URL", NSLocalizedDescription=bad URL} 
2013-01-07 11:48:51.407 Offitel2[22086:907] I restkit.network:RKHTTPRequestOperation.m:152 GET '(null)' 
2013-01-07 11:48:51.408 Offitel2[22086:907] E restkit.network:RKHTTPRequestOperation.m:173 GET '(null)' (0) [0.0010 s]: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x1ed57c50 {NSUnderlyingError=0x20161e70 "bad URL", NSLocalizedDescription=bad URL} 

有人可以幫我嗎?

親切的問候

+0

我使用RKObjectManager編輯我的答案。它有幫助嗎? –

+0

不完全,但它幫助我找到正確的答案!所以謝謝! – Steaphann

+0

不用客氣;) –

回答

0

編輯: 這裏有一個版本,而不RKClient

RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:[NSURL URLWithString:@"http://urllinkcompany.com/"]]; 

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Person class]]; 
[mapping addAttributeMappingsFromDictionary:@{ 
@"data.user.cu_email":  @"cu_email", 
}]; 

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:_txtLogin.text, @"email", _txtPass.text, @"pwd", nil]; 

[objectManager loadObjectsAtResourcePath:@"en/webservice/company-user/login/apikey/key12345678" delegate:self block:^(RKObjectLoader* loader) { 
    loader.objectMapping = mapping; 
    loader.method = RKRequestMethodPOST; 
    loader.params = params; 
}]; 

而實現這兩個RKObjectLoaderDelegate方法:

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects 

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error 
+0

RKCLient在新版本中不存在,我的代理方法也有錯誤 – Steaphann

+0

我的不好!我使用RKObjectManager編輯它 –