使用 RESTKit 执行简单的 json POST
我是 iOS 开发新手,在发出简单的 Json POST 请求时遇到问题。 我有一个包含用户和密码的 NSDictionary,我想将这些值作为 Json 发送到服务器并获得响应。我在不使用 RestKit 的情况下完成了这项工作,但我不知道如何使用 RestKit 来完成同样的任务,而且找不到我想要的一个很好的例子。
- (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:
{
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
}
我知道 RestKit 中有一个 Json 解析器,但我找不到任何有关如何解析 rpcData 字典的文档,我需要使用外部库吗?
现在与服务器的通信正常,但我没有发送预期的内容。我的字典以“key=value?key2=value2...”的方式映射。这是一个非常愚蠢的问题,但我被困住了。
更新
在我写这篇文章时,它可以工作,但 Restkit 已更新,所以我不确定这是否有效,请检查他们的文档
这是我的解决方案问题,当您需要调用服务时,我所做的非常适合使用 RPC API:
1.- 首先在您的对象中,您需要导入 Restkit 和 RKRequestSerialization,这非常重要:
#import <RestKit/RestKit.h>
#import <RestKit/RKRequestSerialization.h>
@interface myObject : NSObject <RKRequestDelegate,RKObjectLoaderDelegate>
2.- 这是登录功能发送帖子:
- (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'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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于较旧的 RestKit
您的委托中可能有类似的内容:
您希望它是:
对于 RestKit v.20:
For older RestKit
You probably have something like this in your delegate:
You want it to be:
For RestKit v.20:
我遇到了同样的问题,这就是为我解决的方法。请注意,在我的场景中,我只想访问 RKRequest。
对我来说关键是最后一行中的“MIMEType:RKMIMETypeJSON”。由于我只想使用 RKRequest,因此我需要设置 MIME 类型。否则,我会使用保罗·塞尚的建议。
I had the same problem, and this is what solved it for me. Note, in my scenario, I only wanted to access RKRequest.
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.