当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - IOS RESTKIT HTTP PUT example

问题描述:

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.. :(

网友答案:

SOKeyValue.h : serialized object used as parameter for your call.

#import <Foundation/Foundation.h>

@interface SOKeyValue : NSObject
@property (nonatomic, retain) NSString* key;
@property (nonatomic, retain) NSString* value;
@end

Here's a simplified code to initialize 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] ];

Now we can implement a service who use PUT. In the object that will implement the call dont forget the restkit delegate 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

In your (.m) :

- (void)putKeyValue 
{

    SOKeyValue *keyvalue = [[SOKeyValue alloc] init];
    keyvalue.key = @"k";
    keyvalue.value = @"2";
    [[RKObjectManager sharedManager] putObject:keyvalue delegate:self];
    [keyvalue release];
}

You can see status code in your trace, and use callback functions :

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;

So i dont have MAC at home, it's diffcult for help you about the code structure. If you have questions do not hesitate.

分享给朋友:
您可能感兴趣的文章:
随机阅读: