这是我需要发布到服务的 json。
{
"deviceToken":"asdfasdfasdf",
"alarm": [
{
"start" "8:30",
"end": "9:30",
"line": "156",
"code": "xxxafsdfasdf",
"station": "asdfa",
"stationLeft": 5,
"available": true,
"times": 2
}]
}
响应数据在警报中只有一个字段“id”:
{
"deviceToken":"asdfasdfasdf",
"alarm": [
{
"id":1,
"start" "8:30",
"end": "9:30",
"line": "156",
"code": "xxxafsdfasdf",
"station": "asdfa",
"stationLeft": 5,
"available": true,
"times": 2
}]
}
然后我定义了两个对象: DeviceAlarm 对象:
@interface DeviceAlarm : NSObject
@property(nonatomic, strong) NSMutableArray *alarm;
@property(nonatomic, copy) NSString *deviceToken;
@end
报警对象:
@interface Alarm : NSObject
@property(nonatomic, copy) NSNumber *id;
@property(nonatomic, copy) NSString *start;
@property(nonatomic, copy) NSString *end;
@property(nonatomic, copy) NSString *code;
@property(nonatomic, copy) NSString *line;
@property(nonatomic, copy) NSString *station;
@property(nonatomic, copy) NSNumber *stationLeft;
@property(nonatomic) BOOL available;
@property(nonatomic, copy) NSNumber *times;
@end
这是我要发布的代码。
DeviceAlarm* devicealarm = [[DeviceAlarm alloc] init];
Alarm* alarm = [[Alarm alloc] init];
alarm.start = @"8:00";
alarm.end = @"9:30";
alarm.line = @"156";
alarm.code = @"fasdfasdf";
alarm.station = @"asdfas";
alarm.stationLeft = @1000;
alarm.available = true;
alarm.times = @1;
devicealarm.alarm = [NSArray arrayWithObjects:alarm , nil];
devicealarm.deviceToken = @"adsfasdfasdf";
RKObjectMapping *alarmMapping = [RKObjectMapping requestMapping];
[alarmMapping addAttributeMappingsFromArray:@[@"start",@"end",@"code",@"station", @"stationLeft",@"available",@"times",@"line"]];
RKObjectMapping *deviceMapping = [RKObjectMapping requestMapping];
RKRelationshipMapping *alarmRelationship = [RKRelationshipMapping
relationshipMappingFromKeyPath:@"alarm"
toKeyPath:@"alarm"
withMapping:alarmMapping];
[deviceMapping addAttributeMappingsFromArray:@[@"deviceToken"]];
[deviceMapping addPropertyMapping:alarmRelationship];
NSString* path = @"/api/alarm/asdfasdf";
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:deviceMapping
objectClass:[DeviceAlarm class]
rootKeyPath:nil];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[DeviceAlarm DeviceAlarmResponseMapping]
pathPattern:nil
keyPath: nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://127.0.0.1:5000/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];
[manager postObject:devicealarm path:path parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Loading mapping result: %@", result);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
然后我检查了服务中的post数据,我发现restkit post一个错误数组json。警报字典丢失。
{
"deviceToken":"asdfasdfasdf",
"alarm": [
"8:30",
"9:30",
"156",
"xxxafsdfasdf",
"asdfa",
5,
true,
2
]
}
请帮帮我~:)