创建 url 时避免对数据进行编码

Ank*_*009 5 java url encoding android retrofit2

我正在尝试像这样拨打电话:

 @GET(AppConstants.BASE_URL + "{category_type}/")
    Call<JsonObject> callCustomFilterApI(@Path("category_type") String type,
                                         @QueryMap(encoded = true)  Map<String,String> fields ,
                                         @Query("page") String pageNo);
Run Code Online (Sandbox Code Playgroud)

但数据中@QueryMap可以有“&” ,因此将其编码为%26

无论如何,“&”不要更改为“%26”。

我尝试过的解决方案:

  1. 这里提到的解决方案

  2. 设置编码=真/假

  3. 还有这个

    @DebDeep 询问:

我将 QueryMap 中的数据传递为:

 private void callCustomFilterSearchPagesApi(String type,  ArrayList<FilterListWithHeaderTitle> customFiltersList, int pageNumber, final ApiInteractor listener) {
        Map<String, String> queryMap = new HashMap<>();

            for (FilterListWithHeaderTitle item: customFiltersList) {

                String pairValue;
                if (queryMap.containsKey(item.getHeaderTitle())){
                    // Add the duplicate key and new value onto the previous value
                    // so (key, value) will now look like (key, value&key=value2)
                    // which is a hack to work with Retrofit's QueryMap

                    String oldValue=queryMap.get(item.getHeaderTitle());
                    String newValue="filters[" + item.getHeaderTitle() + "][]"
                            +oldValue+ "&"+"filters[" + item.getHeaderTitle() + "][]"+item.getFilterItem();
                    pairValue=newValue;
                }else {
                    // adding first time
                    pairValue= item.getFilterItem();
                }
                try {
                    //pairValue= URLEncoder.encode(pairValue, "utf-8");
                   // LoggerUtils.logE(TAG,pairValue);
                    //queryMap.put(item.getHeaderTitle(), Html.fromHtml(pairValue).toString());
                    queryMap.put(item.getHeaderTitle(), pairValue);

                }catch (Exception u){
                    LoggerUtils.crashlyticsLog(TAG,u.getMessage());
                }

            }
            Call<JsonObject> call = TagTasteApplicationInitializer.mRetroClient.callCustomFilterApI(type, queryMap, "1");
            requestCall(call, listener);
    }
Run Code Online (Sandbox Code Playgroud)

Gok*_* KP 1

使用Interceptor并转换%26&

class RequestInterceptor 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

将其设置为您的OkHttp构建器:

OkHttpClient client = new OkHttpClient.Builder();
client.addInterceptor(new RequestInterceptor());
Run Code Online (Sandbox Code Playgroud)