【问题标题】:Placeholder for part of HTTP GET param in Retrofit?Retrofit中部分HTTP GET参数的占位符?
【发布时间】:2018-06-05 16:13:06
【问题描述】:

尝试在Retrofit2接口方法上构建@Get注解:

 @GET("select?q=DH_PROD_ID:{journalId}")
    Call<PdhResult> getJournal(@Path("journalId") String journalId, Callback<PdhResult> resultCallback);

此变体导致错误:

URL query string "q=DH_PROD_ID:{journalId}" must not have replace block

正如您在此处看到的,查询参数 q 具有常量和可变部分。

由于多种原因,不希望为每次调用都在外部构建参数的全部值。

我只想将journalId 替换为提供的String journalId,可以吗?

【问题讨论】:

  • 你还记得你最后做了什么吗?我今天也遇到了同样的问题。

标签: java http get annotations retrofit2


【解决方案1】:

定义方法怎么样

@GET("select")
Call<PdhResult> getJournal(@QueryParam("q") JournalId journalId, Callback<PdhResult> resultCallback)

class JournalId
{
    String id;

    public JournalId(String id)
    {
        if (id == null || !id.startsWith("DH_PROD_ID:"))
        {
            //throw proper exception
        }
        this.id = id.substring("DH_PROD_ID:".length());
    }

    public String getId()
    {
        return id;
    }
}

【讨论】:

  • 不幸的是,有几种方法可以解决这个问题,它会产生很多额外的代码
猜你喜欢
  • 2016-10-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2016-10-01
  • 1970-01-01
  • 2013-05-17
相关资源
最近更新 更多