【问题标题】:URL encoding error in Retrofit 2.0Retrofit 2.0 中的 URL 编码错误
【发布时间】:2017-02-15 06:45:37
【问题描述】:
 @GET("images")
 Call<Example> getimages(@Query("where=item_id") int item_id);

当我在 where 后使用等号将编码为我的服务器不接受的 %3D 时。我希望在我的 api 调用中的 where 之后使用 = 符号.

我的链接是 图片?where=item_id=1

【问题讨论】:

  • 你的链接是什么?是链接images?where=item_id吗?
  • 没有类似的图片?where=item_id=1
  • 这很奇怪。你可以尝试使用路径参数@GET("images?where=item_id={id}") Call&lt;Example&gt; getimages(@Path("id") int id);
  • 已经试过了。它会抛出一个错误,提到使用动态url查询。因为item_id是动态的。
  • 您可以将where=item_id 替换为where%3Ditem_id

标签: android retrofit retrofit2 urlencode cfurl-encoding


【解决方案1】:

试试这个方法:

@GET("images")
Call<Example> getimages(@Query("where") String item_id);

调用这个方法的时候,必须通过这种方式:

Service service = retrofit.create(Service.class);
Call<Example> call = service.getimages("item_id=1");

如果您可以成功调用您的 Api,您可以使用字符串连接动态传递值。

原因: 传递查询参数时,您只需在@Query("") 中写入查询参数,当您调用此方法并将值传递给“item_id”参数时,将在运行时为其分配值getimages 方法。

要了解有关改造的更多信息,请参阅此链接:https://futurestud.io/tutorials/tag/retrofit

【讨论】:

  • 抱歉,我中间有一个 where 子句。
  • @Hitesh 你能贴出你想要调用的网址吗?提及它本身。
  • 你必须使用正斜杠/{path}。我在没有/ 的情况下使用改造,但它仍然可以。
  • 试过这个@GET("images?where=item_id=/{item_id}") 调用 getimages(@Path("item_id")int item_id);并得到错误 URL 查询字符串 "where=item_id=/{item_id}" must not have replace block。对于动态查询参数,请使用@Query。
  • @ChintanSoni 好主意。让我来实现它。
【解决方案2】:

添加编码标志。

@GET("images")
Call<Example> getimages(@Query("where=item_id", encoded = true) String item_id);

并在将 item_id 传递给此方法之前对其进行编码。

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 2016-11-13
    • 2016-07-02
    • 2017-06-11
    • 2021-11-13
    • 2016-09-25
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多