使用RESTKit做一个简单的json POST [英] Do a simple json POST using RESTKit
问题描述
我是iOS开发的新手,我无法创建一个简单的Json POST请求。
我有一个NSDictionary包含用户和密码,我想把这些值作为Json发送到服务器,并得到一个响应。我有工作没有使用restkit,但我不能弄清楚如何使用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;
}
服务器正在等待这样的Json:
The server is expecting a Json like this:
{
jsonrpc : '2.0',
method : 'authenticate', // method name goes here
params : { // params are method-specific
email : 'test@test.com',
password : 'secret'
},
id : 2 // The id can be anything, it will be sent back with the response
}
我知道有一个Json解析器包括在RestKit,但我找不到任何关于如何解析我的rpcData字典的文档,我需要使用一个外部库。
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?.
与服务器它的确定,但我不发送什么是预期的。我的字典映射方式是key = value?key2 = value2 ...。这是一个非常愚蠢的问题,但我被困。
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.
更新
em>当我写这个,它工作,但Restkit已更新,所以我不知道这将工作,请检查他们的文档
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
这里是我的问题的解决方案,我正在做的是理想的工作与RPC API,当你需要调用一个服务:
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.-首先在你的对象你需要导入Restkit和RKRequestSerialization,这是非常重要的:
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.-这是发送帖子的登录函数:
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];
}
}
推荐答案
I有同样的问题,这是什么解决了我。注意,在我的场景中,我只想访问RKRequest。
I had the same problem, and this is what solved it for me. Note, in my scenario, I only wanted to access RKRequest.
NSDictionarty *dict
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
if (!jsonData)
{
NSAssert(FALSE, @"Unable to serialize JSON from NSDict to NSData");
}
else
{
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
request.params = [RKRequestSerialization serializationWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON];
}
我的关键是最后一行中的MIMEType:RKMIMETypeJSON。因为我只想使用RKRequest,这是我需要设置MIME类型。否则,我会使用Paul Cezanne的建议。
The key for me was the 'MIMEType:RKMIMETypeJSON' in the last line. Since I only wanted to use RKRequest, this was how I needed to set the MIME type. Otherwise, I would use Paul Cezanne's suggestion.
这篇关于使用RESTKit做一个简单的json POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!