我想更新在REST API中运行的服务器中的数据。我正在使用ios设备上的RESTKIT。但是我找不到如何在restkit中使用PUT。
我必须发送密钥数据:“user_id”值:“2”这些格式。任何人都可以帮我解决这个问题.. :(
答案 0 :(得分:1)
SOKeyValue.h:用作通话参数的序列化对象。
#import <Foundation/Foundation.h>
@interface SOKeyValue : NSObject
@property (nonatomic, retain) NSString* key;
@property (nonatomic, retain) NSString* value;
@end
这是一个初始化Restkit的简化代码:
/*
This part of code must be executed only one time in your application
*/
//To see logs
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
//Init with good domain
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://mydomain.dev/ui/v1"];
//Indicate to use JSON
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;
//Route path when you call a PUT with SOKeyValue class
[manager.router routeClass:[SOKeyValue class] toResourcePath:@"/yourpath" forMethod:RKRequestMethodPUT];
//Serialization for SOKeyValue class
RKObjectMapping* keyvalueSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class] ];
[authSerializationMapping mapAttributes:@"key", @"value", nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:keyvalueSerializationMapping forClass:[SOKeyValue class] ];
现在我们可以实现使用PUT的服务。在将实现调用的对象中,不要忘记restkit委托 RKObjectLoaderDelegate :
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "SOKeyValue.h"
@interface MyViewOrMyServiceObject: NSObject <RKObjectLoaderDelegate>
- (void)putKeyValue;
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;
@end
在你的(.m):
- (void)putKeyValue
{
SOKeyValue *keyvalue = [[SOKeyValue alloc] init];
keyvalue.key = @"k";
keyvalue.value = @"2";
[[RKObjectManager sharedManager] putObject:keyvalue delegate:self];
[keyvalue release];
}
您可以在跟踪中查看状态代码,并使用回调函数:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;
所以我家里没有MAC,很难帮助你解决代码结构问题。如果您有任何疑问,请不要犹豫。