2013-07-24 28 views
1

我試圖讀取Web服務的簡單的JSON字符串響應:映射一個簡單的字符串RestKit

假設我有這樣的映射:

RKObjectMapping* loginRequestMapping = [RKObjectMapping requestMapping]; 
[loginRequestMapping addAttributeMappingsFromDictionary:@{ 
@"userName" : @"UserName", 
@"password" : @"Password" 
}]; 
RKRequestDescriptor* loginReq = [RKRequestDescriptor requestDescriptorWithMapping:loginRequestMapping objectClass:[LoginRequest class] rootKeyPath:nil method:RKRequestMethodPOST]; 
[objectManager addRequestDescriptor:loginReq]; 

我想這個請求:

LoginRequest* loginReq = [LoginRequest new]; 
loginReq.userName = @"username"; 
loginReq.password = @"1234567890"; 

__block NSString* token; 
[objectManager postObject:loginReq 
        path:@"/sendboxapi/api/login" 
        parameters:nil 
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
          token = [mappingResult firstObject]; 
          NSLog(@"RESULT : %@", token); 
         } 
         failure:^(RKObjectRequestOperation *operation, NSError *error) { 
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                      message:[error localizedDescription] 
                      delegate:nil 
                    cancelButtonTitle:@"OK" 
                    otherButtonTitles:nil]; 
          [alert show]; 
          NSLog(@"Hit error: %@", error); 
         }]; 

的原始響應具有以下格式: 「47c4e389-be2b-466B-B015-c8d5682c4f0a」

我得到此錯誤:命中錯誤:錯誤域= org.restkit.RestKit.ErrorDomain代碼= -1017「加載了內容類型爲'application/json'的無法處理的響應(200)」

什麼是正確的RKObjectMapping設置爲能夠從web服務讀取這個字符串?

謝謝

回答

0

當您使用postObject,RestKit會盡量響應數據應用到源對象(你LoginRequest)。所以你需要添加某個地方的數據來存儲和一個(響應)映射和描述符來處理它。

此外,如果字符串返回給你的是:

"47c4e389-be2b-466b-b015-c8d5682c4f0a" 

那是不是JSON,你就會有試圖處理它是這樣的問題。

+0

但是我如何在我的情況下使用響應?我收到它正確,但我只是需要從某個地方得到它,並避免這種例外! –

+0

將響應更改爲JSON。或者,標題應該說內容類型是純文本,然後您需要設置序列化和映射來處理純文本。 – Wain

+0

所以這是服務器故障... 我仍然認爲RestKit功能強大,但自0.20以來一直不靈活! 舊版本非常靈活! 是否有可能在0.20中做出舊式的RestKit RKRequest? –