Java Retrofit 2 - URL 查询参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36730086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:36:55  来源:igfitidea点击:

Retrofit 2 - URL Query Parameter

javaandroidretrofitretrofit2

提问by Alan

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

我正在使用查询参数来设置 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);

Retrofit generates:

改造产生:

&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

How do I fix this?

我该如何解决?

采纳答案by Andreas

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?a=5"),则 any@Query("b")必须附加 using &,产生类似foobar?a=5&b=7.

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

如果您指定@GET("foobar"),则第一个@Query必须附加 using ?,产生类似foobar?b=7.

That's how Retrofit works.

这就是改造的工作原理。

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

当您指定 时@GET("foobar?"),Retrofit 认为您已经提供了一些查询参数,并使用 附加了更多查询参数&

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

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

回答by Taban Cosmos

I am new to retrofit and I am enjoying it. So here is a simple way to understand it for those that might want to query with more than one query: The ? and & are automatically added for you.

我是改装的新手,我很喜欢它。因此,对于那些可能想要使用多个查询进行查询的人来说,这是一种理解它的简单方法: ? 和 & 会自动为您添加。

Interface:

界面:

 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)
}

It will be called this way. Considering you did the rest of the code already.

它将以这种方式调用。考虑到您已经完成了其余的代码。

  Call<Results> call = service.productList("Whatever", "here", IService.API_KEY);

For example when a query is returned, it will look like this.

例如,当返回查询时,它看起来像这样。

//-> https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434 

Link for full project: Please star etc: https://github.com/Cosmos-it/ILoveZappos

完整项目链接:请star等:https: //github.com/Cosmos-it/ILoveZappos

回答by Keshav Gera

 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)

} 

It will be called this way. Considering you did the rest of the code already.

它将以这种方式调用。考虑到您已经完成了其余的代码。

Call<Results> call = service.getUserDetails("[email protected]", "Password@123");

For example when a query is returned, it will look like this.

例如,当返回查询时,它看起来像这样。

https://api.demo.com/[email protected]&password=Password@123