gpt4 book ai didi

android - Retrofit 从基本 url 中删除查询参数

转载 作者:行者123 更新时间:2023-11-30 05:12:56 24 4
gpt4 key购买 nike

我有用于所有请求的查询参数。它被添加到基本 url,如下所示

private val baseUrl = HttpUrl.Builder()
.scheme("http")
.host("ws.audioscrobbler.com")
.addPathSegment("2.0")
.addPathSegment("")
.addQueryParameter("format", "json")
.addQueryParameter("api_key", "val")
.build()

retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.build()

api服务调用是

 @GET("./")
fun searchTracks(@Query("otherParam") query: String): Call<Any>

正确构建 url 直到进行实际调用。它删除了在基本 url 中添加的查询参数,只保留在服务调用中添加的参数。
在调试中显示,直到在 ExecutorCallAdapterFactory 中调用 delegate.enqueue():"http://ws.audioscrobbler.com/2.0/?format=json&api_key=val&otherParam=val "
显示在日志中(通过拦截器):“http://ws.audioscrobbler.com/2.0/?otherParam=val

知道为什么会发生这种情况以及如何保留参数吗?

最佳答案

您应该在请求拦截器中将查询参数添加到 url。

        OkHttpClient.Builder httpClient =  
new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl originalHttpUrl = original.url();

HttpUrl url = originalHttpUrl.newBuilder()
.addQueryParameter("format", "json")
.addQueryParameter("api_key", "val")
.build();

// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.url(url);

Request request = requestBuilder.build();
return chain.proceed(request);
}
});

关于android - Retrofit 从基本 url 中删除查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437612/

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