0) 我正在使用 Retrofit 2 来处理 Bank API .
1)我有一些接口(interface):
public interface ApiService {
@GET("statdirectory/exchange")
Call<List<MyModel>> getСurrency(@Query("date") String inputDate);
}
2) 当我调用方法 getСurrency(someParametr) 时,其中 someParametr 是字符串,包含“date&json”(例如,“20170917&json”):
ApiService apiService = RetrofitController.getApi();
apiService.getCurrency("20170917&json").enqueue(new Callback<List<MyModel>>() {
@Override
public void onResponse(Call<List<MyModel>> call, Response<List<MyModel>> response) {
call.request().url();
Log.e("URL", call.request().url()+"");
response.code();
Log.e("CODE", response.code()+"");
}
//.....
3) 我看到了:
URL:"https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date=20170917 %26json"(& 替换为 %26)
代码:“404”
4) 在我的界面中我添加了encoded:
getСurrency(@Query(value="date", encoded=false) String inputDate);
但是我的结果和步骤 3 一样!
5) 如何检查这个问题?如何在我的字符串中获取没有 %26 的 URL?我阅读了其他有类似问题的问题,但没有解决我的问题。谢谢!
最佳答案
我只是想澄清一下,最初的问题是编码参数需要为真:encoded=
true。这表明提供的值已经编码,因此不需要通过改造重新编码。正如改造文档中所述,encoded
的默认值为 false。即:
getСurrency(@Query(value="date", encoded=true) String inputDate);
将生成正确的 url。
文档对 encoded
参数作了如下说明:
Specifies whether the parameter name and value are already URL encoded.
来源:https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
关于java - 改造 2 : @Query "encoded=false" don't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277907/