gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:29 24 4
gpt4 key购买 nike

我正在尝试像这样进行调用:

 @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 可以在数据中包含 "&",因此将其编码为 %26

有没有反正"&"不要改成"%26"。

我尝试过的解决方案:

  1. Solution mentioned here

  2. 设置编码=真/假

  3. And also this one .

    @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);
}

最佳答案

使用拦截器并将%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());

关于java - 避免在创建 url 时对数据进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49725862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com