改造 2:@Query "encoded=false";不工作 [英] Retrofit 2: @Query "encoded=false" don't work

查看:42
本文介绍了改造 2:@Query "encoded=false";不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0) 我正在使用 Retrofit 2 来处理 Bank API.
1)我有一些界面:

0) I am using Retrofit 2 for work with Bank API.
1) I have some 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"):

2) And when i call method getСurrency(someParametr), where someParametr is string, consist with "date&json" (for example, "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) 我明白了:
网址:"https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date=20170917%26json"(& 被替换为 %26>)
代码:404"
4)在我的界面中添加编码:

3) I see that:
URL: "https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date=20170917%26json" (& is replaced by %26)
CODE: "404"
4) Inmy interface i add encoded:

getСurrency(@Query(value="date", encoded=false) String inputDate);

但是我的结果与步骤 3 中的结果相同

5) 如何检查这个问题?如何在我的字符串中没有 %26 的情况下获取 URL?我阅读了其他有类似问题的问题,但没有解决我的问题.谢谢!

5) How to check this problem? How to get URL without %26 on my string? I read other questions with similar problem, but isn't solve my problem. Thanks!

推荐答案

如此处所述 https://github.com/square/okhttp/issues/2623 by swankjesse

As noted here https://github.com/square/okhttp/issues/2623 by swankjesse

使用 HttpUrl 构建网址>

Use HttpUrl to build the url

HttpUrl url = HttpUrl.parse("https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?date=20170916&json");

然后将您的方法调用更改为

Then change your method call to

@GET
Call<List<MyModel>>  getСurrency(@Url String ur);

然后

 apiService.getCurrency(url.toString())
       .enqueue(new Callback<List<MyModel>>() {

        @Override
        public void onResponse(Call<List<MyModel>> call, retrofit2.Response<List<MyModel>> response) {
            // your response
        }

        @Override
        public void onFailure(Call<List<MyModel>> call, Throwable t) {

        }

    });

另一种方法是使用 Okhttp 的拦截器并将 %26 替换为 &

Other way is to use Okhttp's Interceptor and replace %26 by &

class MyInterceptor implements Interceptor {
   @Override
   Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request()
    String stringurl = request.url().toString()
    stringurl = stringurl.replace("%26", "&")

    Request newRequest = new Request.Builder()
        .url(stringurl)
        .build()

    return chain.proceed(newRequest)
 }
}

然后

 OkHttpClient client = new OkHttpClient.Builder();
 client.addInterceptor(new MyInterceptor());

这篇关于改造 2:@Query "encoded=false";不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆