2014-10-16 93 views
0

我對iOS開發有點新,所以我試圖找出一個JSON字符串發佈到Web服務器並獲取一些信息。在第一頁中,我要求用戶給我他們的名字和姓氏。然後我爲它們生成一個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