2012-03-01 2 views
0

내가 JSONRestKit : RKObjectManager의 postObject : mapResponseWith가 :

HTTP Headers: { 
    "Content-Length" = 24; 
    "Content-Type" = "application/json"; 
} 
HTTP Body: {"userId":"5","mode":""}. 

으로 개체로 전송하고 JSON을 다시

HTTP Headers: { 
    "Content-Type" = "application/json"; 
} 
HTTP Body: {"insertCount":4,"offerCount":0,"favoriteOfferCount":0,"favoriteInsertCount":0} 

를 얻을 수 및 개체에 매핑 할 위임, 사람이 나를 인도 할 수 있습니다 라우터, 객체 로더 등 많은 오류가 발생합니다.

RKObjectManager의 postObject : mapResponseWith를 사용해야한다는 것을 알고 있습니다. 대표자 :

이지만 구성하는 것은 혼란 스럽습니다. 여기에 신경 쓰지

답변

2

내가 그것을

@interface RestKitUtil : NSObject <RKObjectLoaderDelegate,RKRequestDelegate> 
-(void)postobj; 
@end 


@implementation RestKitUtil 

-(void)postobj{ 

    RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 
    [RKObjectManager objectManagerWithBaseURL:@"http://10.6.10.121:80/CW_war"]; 

    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[CountResultVO class]]; 
    [mapping mapKeyPathsToAttributes: 
    @"offercount", @"offercount", 
    @"insertCount", @"insertCount", 
    @"favouriteOfferCount", @"favouriteOfferCount", 
    @"favouriteInsertCount", @"favouriteInsertCount", 
    nil]; 


    RKObjectRouter *router = [RKObjectManager sharedManager].router; 
    [router routeClass:[BasicUserArgVo class] toResourcePath:@"/getMyOffersCount" forMethod:RKRequestMethodPOST]; 

    [RKObjectManager sharedManager].objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"objStore.sqlite"]; 

    RKObjectMapping *listMapping = [RKObjectMapping mappingForClass:[BasicUserArgVo class]];  
    [listMapping mapKeyPath:@"userId" toAttribute:@"userId"]; 
    [listMapping mapKeyPath:@"mode" toAttribute:@"mode"]; 

    [[RKObjectManager sharedManager].mappingProvider addObjectMapping:listMapping ]; 

    [[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[listMapping inverseMapping] forClass:[BasicUserArgVo class]]; 

    [[RKObjectManager sharedManager] setSerializationMIMEType:RKMIMETypeJSON]; 
    //NSUTF8StringEncoding 
    [[RKObjectManager sharedManager] setAcceptMIMEType:RKMIMETypeJSON]; 


    BasicUserArgVo *bvo = [[BasicUserArgVo alloc] initWithUserId:5]; 

    [[RKObjectManager sharedManager] postObject:bvo mapResponseWith:mapping delegate:self]; 
} 


- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects { 
    CountResultVO* cvo = [objects objectAtIndex:0]; 

    NSLog(@"values:%ld,%ld,%ld,%ld",[cvo offercount],[cvo insertCount], 
      [cvo favouriteOfferCount],[cvo favouriteInsertCount]); 
} 

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error { 
    NSString *msg = [NSString stringWithFormat:@"Error: %@", [error localizedDescription]]; 
    NSLog(@"log : %@",msg);  
} 

@end 
했다 방법