2

I want to update data in server which runs in REST API. i am using RESTKIT from ios device. But i could not find how to use PUT in restkit.

I have to send data like key:"user_id" value:"2" these format. Can anyone please help me to solve this problem.. :(

4

1 回答 1

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,很难帮助你了解代码结构。如果您有任何问题,请不要犹豫。

于 2012-04-09T20:33:05.147 回答