我尝试使用RESTKit
创建此命令curl -X POST -H "Content-Type:application/json" -d '{"rgb":{"cwhite":0,"wwhite":0,"red":22222,"blue":0,"green":22222},"period":1000}' http://192.168.8.7/config?command=light
这是我的代码
映射类
@interface LightState : NSObject
@property (unsafe_unretained, nonatomic) NSInteger period;
@property (strong, nonatomic) LightRGB *rgb;
@end
@interface LightRGB : NSObject
@property (unsafe_unretained, nonatomic) NSInteger red;
@property (unsafe_unretained, nonatomic) NSInteger green;
@property (unsafe_unretained, nonatomic) NSInteger blue;
@property (unsafe_unretained, nonatomic) NSInteger cwhite;
@property (unsafe_unretained, nonatomic) NSInteger wwhite;
@end
发布申请代码
LightState * state = [LightState new];
if ([sender.titleLabel.text isEqualToString:@"Open"]) {
state.period = 1000;
state.rgb.red = 0;
state.rgb.green = 0;
state.rgb.blue = 0;
state.rgb.cwhite = 0;
state.rgb.wwhite = 0;
}else{
state.period = 1000;
state.rgb.red = 22222;
state.rgb.green = 22222;
state.rgb.blue = 22222;
state.rgb.cwhite = 22222;
state.rgb.wwhite = 22222;
}
//ObjectMapping
RKObjectMapping *lightMapping = [RKObjectMapping requestMapping];
[lightMapping addAttributeMappingsFromDictionary:@{@"period":@"period"}];
RKObjectMapping *lightRGBMapping = [RKObjectMapping requestMapping];
[lightRGBMapping addAttributeMappingsFromDictionary:@{@"red":@"red",@"green":@"green",@"blue":@"blue",@"cwhite":@"cwhite",@"wwhite":@"wwhite"}];
[lightMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"rgb"
toKeyPath:@"rgb"
withMapping:lightRGBMapping]];
RKRequestDescriptor *lightRD = [RKRequestDescriptor requestDescriptorWithMapping:lightMapping objectClass:[LightState class] rootKeyPath:nil method:RKRequestMethodPOST];
//URL
NSString *urlStr = [NSString stringWithFormat:@"http://%@",self.deviceIP];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:urlStr]];
[client setDefaultHeader:@"Content-Type" value:@"application/json"];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
RKObjectManager *obj = [[RKObjectManager alloc]initWithHTTPClient:client];
[obj addRequestDescriptor:lightRD];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
//post
[obj postObject:state path:@"/config?command=light" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"lightToggleHandler Success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"lightToggleHandler Failure header\n%@ body\n%@",operation.HTTPRequestOperation.request.allHTTPHeaderFields,operation.HTTPRequestOperation.request.HTTPBody);
}];
}
以下是RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
打印
request.headers={
"Accept-Language" = "zh-TW;q=1, en-TW;q=0.9, zh-Hans-TW;q=0.8, ja-TW;q=0.7";
"Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
"User-Agent" = "LightController/1 (iPhone; iOS 9.3.2; Scale/2.00)";
}
request.body=period=1000&rgb[blue]=0&rgb[cwhite]=0&rgb[green]=0&rgb[red]=0&rgb[wwhite]=0
请求正文看起来正确,但服务器返回错误
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Loaded an unprocessable error response (400)"
哪里错了?任何人都有任何想法正确地做到这一点?