Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

objective c - Do a simple json POST using RESTKit

I'm new to iOS development and I'm having trouble making a simple Json POST request. I have a NSDictionary containing an user and password and I want to send those values as a Json to a server and get a response. I had that working without using restkit but I can't figure out how to accomplish the same using RestKit and just can't find a good example of what I want.

- (bool) login{

    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
    [params setValue:self.email forKey:@"email"];
    [params setValue:self.password forKey:@"password"];    

    NSMutableDictionary* rpcData = [[NSMutableDictionary alloc] init];
    [rpcData setValue:@"2.0" forKey:@"jsonrpc"];
    [rpcData setValue:@"authenticate" forKey:@"method"];
    [rpcData setValue:@"" forKey:@"id"];
    [rpcData setValue:params forKey:@"params"];

    [[RKClient sharedClient] post:@"/api/rpc/" params:rpcData delegate:self];
    return nil;
}

The server is expecting a Json like this:

{
    jsonrpc : '2.0',
    method : 'authenticate', // method name goes here
    params : {  // params are method-specific
        email : '[email protected]',
        password : 'secret'
    },
    id : 2  // The id can be anything, it will be sent back with the response
}

I understand that there is a Json parser include in RestKit but I can't find any documentation on how to parse my rpcData dictionary, do I need to use an external library?.

Right now the communication with the server it's ok, but I'm not sending what is expected. My dictionary is mapped in the way "key=value?key2=value2...". This is very silly question but I'm stucked.

Update

By the time I wrote this, it worked but Restkit has been updated so I'm not sure if this will work, please check their documentation

Here is the solution to my problem, what I'm doing is ideal for working with RPC APIs when you need to call a service:

1.- First in your object you need to import Restkit and RKRequestSerialization, this is very important:

#import <RestKit/RestKit.h>
#import <RestKit/RKRequestSerialization.h>

@interface myObject : NSObject <RKRequestDelegate,RKObjectLoaderDelegate>

2.- Here is the login function sending the post:

- (void) login:(NSString *)username :(NSString *)password{

    RKClient *myClient = [RKClient sharedClient];
    NSMutableDictionary *rpcData = [[NSMutableDictionary alloc] init ];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];

    //User and password params
    [params setObject:password forKey:@"password"];
    [params setObject:username forKey:@"email"];

    //The server ask me for this format, so I set it here:
    [rpcData setObject:@"2.0" forKey:@"jsonrpc"];
    [rpcData setObject:@"authenticate" forKey:@"method"];
    [rpcData setObject:@"" forKey:@"id"];
    [rpcData setObject:params forKey:@"params"];

    //Parsing rpcData to JSON! 
    id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
    NSError *error = nil;
    NSString *json = [parser stringFromObject:rpcData error:&error];    

    //If no error we send the post, voila!
    if (!error){
        [[myClient post:@"/" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self] send];
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For older RestKit

You probably have something like this in your delegate:

    objectManager.serializationMIMEType = RKMIMETypeFormURLEncoded;

You want it to be:

    objectManager.serializationMIMEType = RKMIMETypeJSON;

For RestKit v.20:

    // thanks  ColdLogic (from his comment)
    [objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...