在创建网址时避免编码数据

时间:2018-04-09 05:03:06

标签: java android url encoding 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);

但是@QueryMap可以在数据中包含“&amp;”,因此将其改造为%26

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

我试过的解决方案:

  1. Solution mentioned here
  2. 设置编码= true / false
  3. And also this one

    @DebDeep问:

  4. 我在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);
        }
    

1 个答案:

答案 0 :(得分: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);
    }
}

将此设置为OkHttp构建器:

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