如何配置广场的改造客户端来处理与参数可变数量的请求 [英] How can I configure Square's Retrofit Client to handle a request with a variable number of parameters

查看:152
本文介绍了如何配置广场的改造客户端来处理与参数可变数量的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Android应用程序,并在用广场的改造库短暂的网络电话。我是比较新的Java和Android系统。到现在为止我已经构造的要求,像这样:

I'm building an Android App and am using Square's Retrofit library for short-lived network calls. I'm relatively new to Java and Android. Until now I've constructed requests like so:

@GET("/library.php")
void library(
        @Query("one_thing") String oneThing,
        @Query("another_thing") String anotherThing,
        Callback<Map<String,Object>> callback
);

和叫他们像这样:

    service.library(oneThing, anotherThing, callback);

我需要实现接受可变数量的参数,不超过10个左右的请求。它的麻烦必须单独定义它们,并通过或者是不给定请求present的那些东西。有没有一种方法来定义一个请求的接口,使得它接受在参数字典/映射每个元素的可变数目或参数和自动构造 @Query S'事情是这样的:

I need to implement a request that accepts a variable number of parameters, not more than 10 or so. It's cumbersome to have to define them individually and pass null or something for the ones that aren't present for a given request. Is there a way to define an interface for a request such that it accepts a variable number or parameters and auto-constructs @Querys for each element in the parameter dictionary/map? Something like this:

@GET("/library.php")
void library(
        Map<String,Object> parameters,
        Callback<Map<String,Object>> callback
);

service.library(parameters, callback);

先谢谢您的任何提示。

Thanks in advance for any tips.

编辑:传递null对于那些不相关的请求不会在这种情况下工作PARAMS。理想情况下,我会能够设置/创建 @Query s为基础的参数字典,使按键不会成为 @Query 如果他们的值为null。

passing null for params that aren't pertinent to the request wont work in this case. Ideally I'd be able to set/create @Querys based on the parameter dictionary, so that keys wont become a @Query if their value is null.

编辑:我专门寻找与GET请求有效的解决方案。

I'm specifically looking for a solution that works with GET requests.

推荐答案

这是一个有点晚了,但无论如何,我会张贴以防万一有人发现了同样的问题,谷歌。

It's a bit late but I'll post it anyway just in case someone found the same problem on Google.

他们已经推出了注释@QueryMap

They've introduced the Annotation @QueryMap

此注解让你通过,并实现地图类,是不是像你一样,它可以让POST请求你传递一个对象作为参数对象,但它得到工作完成。

This annotation let you pass and object that implements Map class, is not as nice as Post requests that let's you pass an Object as parameter but it get the works done.

@GET("/library.php")
public void library(@QueryMap Map<String, Object> parameters, Callback<Map<String,Object>> callback);

这篇关于如何配置广场的改造客户端来处理与参数可变数量的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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