改造 2 - URL 查询参数
Retrofit 2 - URL Query Parameter
我正在使用查询参数来设置 Google 地图 API 所需的值。
问题是第一个查询参数不需要 &
符号。
@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
当我需要它时导致调用失败
address=90210&sensor=false&client=gme-client&signature=signkey
我该如何解决这个问题?
如果您指定 @GET("foobar?a=5")
,则必须使用 &
附加任何 @Query("b")
,生成类似 foobar?a=5&b=7
.
的内容
如果您指定 @GET("foobar")
,则必须使用 ?
附加第一个 @Query
,生成类似 foobar?b=7
.
的内容
这就是 Retrofit 的工作原理。
当你指定 @GET("foobar?")
时,Retrofit 认为你已经提供了一些查询参数,并使用 &
.
附加 more 个查询参数
删除?
,你会得到想要的结果。
我是改造新手,我很享受。因此,对于那些可能想要使用多个查询进行查询的人来说,这是一种理解它的简单方法: ?和 & 会自动为您添加。
接口:
public interface IService {
String BASE_URL = "https://api.test.com/";
String API_KEY = "SFSDF24242353434";
@GET("Search") //i.e https://api.test.com/Search?
Call<Products> getProducts(@Query("one") String one, @Query("two") String two,
@Query("key") String key)
}
会这样调用。考虑到您已经完成了其余代码。
Call<Results> call = service.productList("Whatever", "here", IService.API_KEY);
例如,当查询返回时,它看起来像这样。
//-> https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434
Link 至完整项目:请加注星标等:https://github.com/Cosmos-it/ILoveZappos
如果您觉得有用,请不要忘记给它加注星标。 :)
public interface IService {
String BASE_URL = "https://api.demo.com/";
@GET("Login") //i.e https://api.demo.com/Search?
Call<Products> getUserDetails(@Query("email") String emailID, @Query("password") String password)
}
会这样调用。考虑到您已经完成了其余代码。
Call<Results> call = service.getUserDetails("abc@gmail.com", "Password@123");
例如,当查询返回时,它看起来像这样。
https://api.demo.com/Login?email=abc@gmail.com&password=Password@123
我正在使用查询参数来设置 Google 地图 API 所需的值。
问题是第一个查询参数不需要 &
符号。
@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
当我需要它时导致调用失败
address=90210&sensor=false&client=gme-client&signature=signkey
我该如何解决这个问题?
如果您指定 @GET("foobar?a=5")
,则必须使用 &
附加任何 @Query("b")
,生成类似 foobar?a=5&b=7
.
如果您指定 @GET("foobar")
,则必须使用 ?
附加第一个 @Query
,生成类似 foobar?b=7
.
这就是 Retrofit 的工作原理。
当你指定 @GET("foobar?")
时,Retrofit 认为你已经提供了一些查询参数,并使用 &
.
删除?
,你会得到想要的结果。
我是改造新手,我很享受。因此,对于那些可能想要使用多个查询进行查询的人来说,这是一种理解它的简单方法: ?和 & 会自动为您添加。
接口:
public interface IService {
String BASE_URL = "https://api.test.com/";
String API_KEY = "SFSDF24242353434";
@GET("Search") //i.e https://api.test.com/Search?
Call<Products> getProducts(@Query("one") String one, @Query("two") String two,
@Query("key") String key)
}
会这样调用。考虑到您已经完成了其余代码。
Call<Results> call = service.productList("Whatever", "here", IService.API_KEY);
例如,当查询返回时,它看起来像这样。
//-> https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434
Link 至完整项目:请加注星标等:https://github.com/Cosmos-it/ILoveZappos
如果您觉得有用,请不要忘记给它加注星标。 :)
public interface IService {
String BASE_URL = "https://api.demo.com/";
@GET("Login") //i.e https://api.demo.com/Search?
Call<Products> getUserDetails(@Query("email") String emailID, @Query("password") String password)
}
会这样调用。考虑到您已经完成了其余代码。
Call<Results> call = service.getUserDetails("abc@gmail.com", "Password@123");
例如,当查询返回时,它看起来像这样。
https://api.demo.com/Login?email=abc@gmail.com&password=Password@123