2013-07-15 18 views
1

所以,我第一次發送POST請求。我M映射類和我想的和從文檔,它會以這種方式工作閱讀:使用RestKit發送POST沒有返回JSON內容

初始化RK:

- (void)initRK{ 
    if(!manager){ 
     manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_CONTEXT_URL]]; 
    } 

    if (!reqMapping) { 
     reqMapping = [RKObjectMapping requestMapping]; 
    } 
} 

POST方法:

// Configure a request mapping for our Article class. We want to send back title, body, and publicationDate 
RKObjectMapping* deviceRequestMapping = [RKObjectMapping mappingForClass:[DeviceDTO class]]; 
[deviceRequestMapping addAttributeMappingsFromArray:@[ @"model", @"name", @"systemName", @"systemVersion", @"devToken" ]]; 

RKObjectMapping* msRequestMapping = [RKObjectMapping mappingForClass:[MemberShipDTO class]]; 
[msRequestMapping addAttributeMappingsFromArray:@[ @"validSince", @"validTill" ]]; 

RKObjectMapping* countryRequestMapping = [RKObjectMapping mappingForClass:[CountryDTO class]]; 
[countryRequestMapping addAttributeMappingsFromArray:@[ @"idNumberDTO", @"iso2DTO", @"short_nameDTO", @"calling_codeDTO" ]]; 

RKObjectMapping* userRequestMapping = [RKObjectMapping requestMapping]; 
[userRequestMapping addAttributeMappingsFromArray:@[ @"displayName", @"phoneNumber", @"status", @"userID" ]]; 

[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"device" withMapping:deviceRequestMapping]]; 
[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"memberShip" withMapping:msRequestMapping]]; 
[userRequestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"country" withMapping:countryRequestMapping]]; 

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMapping objectClass:[User class] rootKeyPath:@"user"]; 


//Create Objects 
UserDTO *user = [[UserDTO alloc]init]; 
user.displayName = userDTO.displayName; 
user.phoneNumber = userDTO.phoneNumber; 
user.status = userDTO.status; 
user.userID = userDTO.userID; 
user.country = userDTO.country; 

DeviceDTO *device = [[DeviceDTO alloc]init]; 
device.name = devDTO.name; 
device.systemName = devDTO.systemName; 
device.systemVersion = devDTO.systemVersion; 
device.model = devDTO.model; 
device.devToken = [[NSUserDefaults standardUserDefaults]objectForKey:PUSHTOKEN_USER_DEFAULTS_KEY]; 

user.deviceInfo = device; 

MemberShipDTO *ms = [[MemberShipDTO alloc]init]; 
ms.validSince = [NSDate date]; 
ms.validTill = [[UtilitieHandler new] getDateByAdd:+1 :0 :0 :0]; 

user.memberShipDetails = ms; 

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/json"]; 

[[RKObjectManager sharedManager] setRequestSerializationMIMEType:RKMIMETypeJSON]; 
[[RKObjectManager sharedManager] setAcceptHeaderWithMIMEType:RKMIMETypeJSON]; 
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor]; 
[[RKObjectManager sharedManager] postObject:user path:@"user/integrate" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){ 
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array); 
}failure:^(RKObjectRequestOperation *operation, NSError *error) { 
    RKLogError(@"Operation failed with error: %@", error); 
}]; 

所以我嘗試了不同的事情,並在使用wireshark捕獲請求後返回沒有內容發送的請求。這意味着映射工作不正確。我嘗試了很多,沒有任何幫助。任何建議都會很棒!

這裏捕獲的數據包:

POST /WAZZUUPWS/rest/service/user/integrate HTTP/1.1 
Host: 192.168.2.115:8080 
Accept-Encoding: gzip, deflate 
Accept: application/json 
Content-Length: 0 
Connection: keep-alive 
Accept-Language: de;q=1, en;q=0.9, fr;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5 
User-Agent: WAZZUUP!/1.0 (iPhone; iOS 6.1.4; Scale/2.00) 
+0

'requestDescriptor'似乎沒有鏈接到'UserDTO'類。這是一個錯字嗎? – Wain

+0

哥們我剛纔看到了。我會檢查,當我在家裏。我希望這是問題!寫一個答案,我可以給它的信用,如果它的真實! – SaifDeen

回答

3

它可能只是您的問題中的拼寫錯誤,但requestDescriptor似乎沒有鏈接到UserDTO類。

+0

遇到了一些問題! ty非常 – SaifDeen

0

好像你沒有核心數據的理解尚對象。使用核心數據持久化的對象是NSManagedObject的子類,必須以不同的方式創建。進一步閱讀此鏈接: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/Articles/cdCreateMOs.html

至於當前的問題,你必須使用它代替:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserDTO" inManagedObjectContext:[RKManagedObjectStore defaultStore].mainQueueManagedObjectContext]; 

UserDTO *user = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil]; 

但是,如果UserDTO是NSObject的子類,這將需要更改爲NSManagedObject。

我的工作流程就像這樣 - 創建核心數據模型並使用mogenerator自動生成NSManagedObject類定義。閱讀更多關於它的地方:http://raptureinvenice.com/getting-started-with-mogenerator/

+0

我的Qeustion與Coredata沒有任何關係,Sry。在輸入錯誤之後, – SaifDeen

相關問題