改造2 - URL查询参数 [英] Retrofit 2 - URL Query Parameter
问题描述
我使用查询参数来设置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屋!