RestKit GET查询参数 [英] RestKit GET query parameters

查看:139
本文介绍了RestKit GET查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用RestKit 0.10.0,直到现在,我只将序列化的对象发布到我的服务器上:

  [[RKObjectLoader * loader] {
loader.delegate = self;
loader.objectMapping = responseMapping;
loader.serializationMIMEType = RKMIMETypeFormURLEncoded;
loader.targetObject = nil;
}];

到目前为止,这么好。但是我现在需要用几个查询参数向服务器发出一个GET请求。第一个自然的想法就是像我一样发布对象:


  • 创建对象封装的序列化映射查询参数
  • 为从服务器接收的对象创建响应映射
  • 为RKRequestMethodGET定义并使用路由器(而不是RKRequestMethodPOST)
  • / li>
  • 使用getObject:usingBlock(而不是postObject:usingBlock)创建请求



这不是做这件事的方法,所以在搜索可用资源( RestKit Wiki RestKit Google小组)我现在知道有两种解决方案被认为是有效的:






这个功能非常完美。

  NSDictionary * queryParams = [NSD词典dictionaryWithObjectsAndKeys:
token,@accessToken,
[NSNumber numberWithInt:level],@level,
[NSNumber numberWithInt:count],@count,
零];

NSString * resourcePath = [PEER_SUGGESTIONS_CONTROLLER_PATH stringByAppendingQueryParameters:queryParams];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath
usingBlock:^(RKObjectLoader * loader){
loader.delegate = self;
loader.objectMapping = responseMapping;
}];




  • 在加载程序块中设置查询参数。


  • 这不会发送查询参数。

      RKParams * params = [RKParams params]; 
    [params setValue:token forParam:@accessToken];
    [params setValue:[NSNumber numberWithInt:level] forParam:@level];
    [params setValue:[NSNumber numberWithInt:count] forParam:@count];
    $ b $ [RKObjectManager sharedManager] loadObjectsAtResourcePath:PEER_SUGGESTIONS_CONTROLLER_PATH
    usingBlock:^(RKObjectLoader * loader){
    loader.delegate = self;
    loader.objectMapping = responseMapping;
    loader.params = params;
    }];

    我的问题是:


    1. 为什么第二个解决方案无法工作?

    2. 为什么第一个解决方案不需要将loader.targetObject设置为零,尽管我没有任何根密钥JSON响应中的路径?

    3. 什么情况下我应该使用getObject:usingBlock方法?它的目的是什么?

    4. 我应该使用loader.params作什么?来自 wiki 的对象映射教程说这个属性可以用来封装POST参数,但是我没有因为我可以将参数封装在使用方法postObject:usingBlock发送的序列化对象中,因此请参阅这一点。

    谢谢。 / p>

    [已编辑]



    关于第二个问题的答案:我一直将targetObject设置为零在发送POST请求的时候在加载程序块中,否则RestKit会尝试使用发送对象映射作为响应(检查链接进行相关讨论)。但是由于我使用的是loadObjectsAtResourcePath:usingBlock:,因此没有对象正在发送,因此响应自然会映射到响应映射,而无需将setObject设置为零。

    解决方案



    1. 为什么第二个解决方案不能正常工作?


    params用于创建一个HTTP正文,它不用于GET / HEAD请求。



    1. 为什么第一个解决方案无需将loader.targetObject设置为nil,尽管我在JSON响应中没有任何根密钥路径
      ? / li>


    我认为targetObject默认为零。您通常不设置它,如果需要,请求会创建它。我使用它的唯一时间是请求没有主键或其他奇怪问题的对象。



    1. 什么是我应该使用getObject:usingBlock方法的情况?它的目的是什么?


    这是一种方便的方法,所以您不必记住所有正确的语法。在内部它只是使用GET发送一个对象加载请求。



    编辑:



    如果你有一个对象你想更新。



    1. 我应该使用loader.params做些什么?来自维基的对象映射教程说这个属性可以用来封装POST
      参数,但是我没有看到这个观点,因为我可以将参数
      包装在与该方法一起发送的序列化对象中
      postObject:usingBlock。


    无论你在params中输入什么,都会被序列化为一个HTTP身体(或身体流)。同样,postObject:usingBlock:只是一个方便的方法,所以你不必记住所有事情。



    RestKit是开源的。如果你不确定它是如何工作的,你可以自由地遵循内部参数。如果您的应用程序和Web服务设计良好,则应该能够使用便捷方法。有时你不能,然后你可以像你所做的那样使用原始表格。

    编辑:
    Q Hrm,引用你的项目符号点数...


    I've been using RestKit 0.10.0 for a while now and up until this point, I only posted serialized objects to my server:

    [[RKObjectManager sharedManager] postObject:serializedObject
                                     usingBlock:^(RKObjectLoader *loader) {
                                         loader.delegate = self;
                                         loader.objectMapping = responseMapping;
                                         loader.serializationMIMEType = RKMIMETypeFormURLEncoded;
                                         loader.targetObject = nil;
                                     }];
    

    So far, so good. But I now need to make a GET request to the server with a few query parameters. The first natural thing that came in mind was to do the same as I did for posting objects:

    • create a serialization mapping for the object encapsulating the query parameters
    • create a response mapping for the object being received from the server
    • define and use a router for RKRequestMethodGET (instead of RKRequestMethodPOST)
    • make the request using getObject:usingBlock (instead of postObject:usingBlock)

    I soon found out this is not the way to do it, so after searching the available resources (RestKit Wiki, RestKit Google group) I now know of two solutions considered as valid:

    • Appending the query parameters to the resource path.

    This works perfectly.

    NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                              token, @"accessToken",
                                              [NSNumber numberWithInt:level], @"level",
                                              [NSNumber numberWithInt:count], @"count",
                                              nil];
    
    NSString* resourcePath = [PEER_SUGGESTIONS_CONTROLLER_PATH stringByAppendingQueryParameters:queryParams];
    
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath
                                                    usingBlock:^(RKObjectLoader *loader) {
                                                        loader.delegate = self;
                                                        loader.objectMapping = responseMapping;
                                                    }];
    

    • Setting the query parameters in the loader block.

    This does not send the query parameters.

    RKParams *params = [RKParams params];
    [params setValue:token forParam:@"accessToken"];
    [params setValue:[NSNumber numberWithInt:level] forParam:@"level"];
    [params setValue:[NSNumber numberWithInt:count] forParam:@"count"];
    
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:PEER_SUGGESTIONS_CONTROLLER_PATH
                                                    usingBlock:^(RKObjectLoader *loader) {
                                                        loader.delegate = self;
                                                        loader.objectMapping = responseMapping;
                                                        loader.params = params;
                                                    }];
    

    My questions are:

    1. Why doesn't the second solution work?
    2. Why is the first solution working without having to set the loader.targetObject to nil, although I do not have any root key path in the JSON response?
    3. What are the cases where I should use the getObject:usingBlock method? What is its intended purpose?
    4. What should I use loader.params for? The object mapping tutorial from the wiki says this property can be used to encapsulate POST parameters, but I do not see the point since I can wrap the parameters in the serialized object that is being sent with the method postObject:usingBlock.

    Thanks.

    [LATER EDIT]

    Regarding the answer to my second question: I've been setting the targetObject to nil in the loader block when making POST requests beacause otherwise RestKit will try use the send object mapping for the response (check this link for a related discussion). But since I am using loadObjectsAtResourcePath:usingBlock:, there is no object being sent, therefore the response will naturally map on the response mapping without having to the set targetObject to nil.

    解决方案

    1. Why doesn't the second solution work?

    params is used to create a HTTP body, which is not used in a GET/HEAD request.

    1. Why is the first solution working without having to set the loader.targetObject to nil, although I do not have any root key path in the JSON response?

    I think targetObject is nil by default. You normally don't set it, the request will create it if needed. The only time I use it is when requesting objects without primary keys or other weird problems.

    1. What are the cases where I should use the getObject:usingBlock method? What is its intended purpose?

    This is a convenience method so you don't have to remember all the correct syntax. Internally it just sends an object load request using GET.

    EDIT:

    Use this if you have an object you want to update.

    1. What should I use loader.params for? The object mapping tutorial from the wiki says this property can be used to encapsulate POST parameters, but I do not see the point since I can wrap the parameters in the serialized object that is being sent with the method postObject:usingBlock.

    Whatever you put in params will be serialized to an HTTP body (or body stream). Again, postObject:usingBlock: is just a convenience method so you don't have to remember everything.

    RestKit is open source. If you are not sure how it works you are free to follow the parameters internally. If you app and web service is well designed, you should be able to use the convenience methods. Sometimes you can not, and then you can use the raw forms like you have done.

    EDIT: Q Hrm, quoting your bullet points messed up the numbers...

    这篇关于RestKit GET查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆