改造2 - URL查询参数 [英] Retrofit 2 - URL Query Parameter

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

问题描述

我使用查询参数来设置Google Maps API所需的值。

I am using a query parameters to set the values needed by the Google Maps API.

问题是我不需要& ; 签署第一个查询参数。

The issue is I do not need the & sign for the first query parameter.

@GET("/maps/api/geocode/json?")
    Call<JsonObject> getLocationInfo(@Query("address") String zipCode,
                                             @Query("sensor") boolean sensor,
                                             @Query("client") String client,
                                             @Query("signature") String signature);

改造产生:

&address=90210&sensor=false&client=gme-client&signature=signkey

当我需要它时会导致呼叫失败

which causes the call the fail when I need it to be

address=90210&sensor=false&client=gme-client&signature=signkey

如何解决这个问题?

推荐答案

如果你指定 @GET(foobar?a = 5),那么任何 @Query(b)必须使用& 附加,产生类似 foobar?a = 5& b = 7

If you specify @GET("foobar?a=5"), then any @Query("b") must be appended using &, producing something like foobar?a=5&b=7.

如果指定 @GET(foobar),然后必须使用附加第一个 @Query ,产生类似 foobar的东西? b = 7

If you specify @GET("foobar"), then the first @Query must be appended using ?, producing something like foobar?b=7.

这就是Retrofit的工作方式。

That's how Retrofit works.

当你指定 @GET(foobar?),Retrofit认为你已经提供了一些查询参数,并且appen ds more 查询参数使用&

When you specify @GET("foobar?"), Retrofit thinks you already gave some query parameter, and appends more query parameters using &.

删除,您将获得所需的结果。

Remove the ?, and you will get the desired result.

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

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