2014-10-16 2 views
0

저는 iOS 개발에 다소 익숙합니다. 그래서 JSON 문자열을 웹 서버에 게시하고 정보를 다시 얻으려고합니다. 첫 페이지에서 나는 사용자가 나를 그들의 이름과 성을 알려줄 것을 요청합니다. 그런 다음 ID를 생성하여 두 번째 페이지로 전달합니다. 두 번째 페이지가로드되면 서버에 JSON 문자열을 보내 사용자에게 성, 성 등을 알려줍니다. 같은 것 요청은 다음과 같습니다CoreData에서 POST JSON 서버로 정보

{ 
"user":{ 
    "first_name":"Joe", 
    "last_name":"User", 
    "device_descriptor":"iPhone", 
    "idfa":"12345678", 
    "btmacid":"01:23:45:67:89:ab" 
    } 
} 

다음과 같이 다시 올 것이다 성공적인 응답을 만들 :

{ 
    "id": "8", 
    "first_name: "Joe", 
    "last_name": "User", 
    "device_descriptor": "iPhone", 
    "created_at": "2014-10-07T05:25:36.119Z", 
    "updated_at": "2014-10-07T05:25:36.119Z", 
    "idfa": "12345678", 
    "btmacid": "01:23:45:67:89:ab" 
} 

내가 지금까지 다음되는대로 코드 :

#import "WelcomeViewController.h" 
#import "CoreDataStack.h" 
#import "UserInformation.h" 
#import <CoreData/CoreData.h> 
#import <RestKit/RestKit.h> 

@interface WelcomeViewController() 

@property (nonatomic,strong) NSFetchedResultsController *fetchResultsController; 

@property (strong, nonatomic) IBOutlet UILabel *welcomeTextLabel; 

@end 

@implementation WelcomeViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [self.fetchResultsController performFetch:nil]; 

    UserInformation *entry = [[self.fetchResultsController fetchedObjects] objectAtIndex:0]; 

    _welcomeTextLabel.text = [NSString stringWithFormat: @"Welcome " @"%@!", entry.firstName]; 

    [self postUserInformation]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSFetchRequest *)entryFetchRequest{ 
    NSFetchRequest *fetchReqeust = [NSFetchRequest fetchRequestWithEntityName:@"UserInformation"]; 

    fetchReqeust.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO]]; 

    return fetchReqeust; 
} 

- (NSFetchedResultsController *)fetchResultsController { 
    if (_fetchResultsController != nil) { 
     return _fetchResultsController; 
    } 

    CoreDataStack *coreDataStack = [CoreDataStack defaultStack]; 
    NSFetchRequest *fetchRequest = [self entryFetchRequest]; 

    _fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 

    return _fetchResultsController; 
} 

- (void) postUserInformation { 
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 

    [self.fetchResultsController performFetch:nil]; 

    UserInformation *entry = [[self.fetchResultsController fetchedObjects] objectAtIndex:0]; 


    RKObjectManager *manager = [RKObjectManager sharedManager]; 
    RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[UserInformation class]]; 
    NSDictionary *userInformation = @{ 
             @"id"     : @"userID", 
             @"first_name"   : @"firstName", 
             @"last_name"   : @"lastName", 
             @"device_descriptor" : @"deviceHardwareID", 
             @"created_at"   : @"created_at", 
             @"updated_at"   : @"updated_at", 
             @"idfa"    : @"deviceAdvertisementID", 
             @"btmacid"   : @"btmac_Id" 
             }; 
    [responseMapping addAttributeMappingsFromDictionary:userInformation]; 

    manager.requestSerializationMIMEType = RKMIMETypeJSON; 

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"/users" keyPath:nil statusCodes:statusCodes]; 

    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
    [requestMapping addAttributeMappingsFromDictionary:userInformation]; 

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[userInformation class] rootKeyPath:nil method:RKRequestMethodAny]; 

    [manager addResponseDescriptor:responseDescriptor]; 
    [manager addRequestDescriptor:requestDescriptor]; 

// NSDictionary *params = @{ 
//        @"id"     : entry.userID, 
//        @"first_name"   : entry.firstName, 
//        @"last_name"   : entry.lastName, 
//        @"device_descriptor" : entry.deviceHardwareID, 
//        @"created_at"   : entry.createdAt, 
//        @"updated_at"   : entry.updatedAt, 
//        @"idfa"    : entry.deviceAdvertisementID, 
//        @"btmacid"   : entry.btmacID 
//        }; 

    [manager postObject:userInformation path:@"http://serverendpoint.com/users" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
     NSLog(@"Success"); 
    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"Failure: %@", error); 
    }]; 

} 


/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 

@end 

어떤 도움을 당신 모두가 제공 할 수있는 것은 크게 감사 할 것입니다. 입력과 출력은 약 잘못된 방법이기 때문에

답변

0

당신은

[requestMapping addAttributeMappingsFromDictionary:userInformation]; 

에 요청 맵핑을 작성해서는 안된다. 대신 응답 매핑의 inverseMapping을 사용하십시오.