翻新 - 在插入查询后追加文本到终点的末尾
问题描述:
我想生成类似于/api/method?page=1.json
的终点,但问题是查询将插入到末尾,就像/api/method.json?page=1
一样。翻新 - 在插入查询后追加文本到终点的末尾
// from my service
@GET("method.json")
Call<Void> method(@Query("page") int page);
// building retrofit
new Retrofit.Builder()
.baseUrl(URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
更新/以下的工作,但我@Blackbelt回答是最好的一个。
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BASIC : HttpLoggingInterceptor.Level.NONE);
return new OkHttpClient.Builder()
.addInterceptor(logging)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder().url(request.url().toString().concat(".json")).build();
return chain.proceed(request);
}
})
.build();
答
我想产生终点喜欢/api/method?page=1.json
原因。没有.json
它看起来更好,你以某种方式隐藏了响应是一个json
对象的信息。
but the problem is queries will be inserted at the end just like /api/method.json?page=1.
这是预期的行为。正如你看到它附加你提供的参数。所以,你可以从
Call<Void> method(@Query("page") int page);
改变
Call<Void> method(@Query("page") String page);
然后调用它像
method(String.valueOf(page).concat(".json"));
答
您可以使用类似,
@GET("method?page={page}.json")
Call<Void> method(@Path("page") int page);
+0
它有帮助,但它是一个丑陋的解决方案。 '@ Query'必须被使用。 – Alireza
答
简单地试图改变像
@GET("method.json")
Call<Void> method(@Query("page") String page);
,并通过价值类似方法(“1.json”)
我认为这是一个丑陋的解决方案。为什么我这样做是因为CakePHP以JSON的形式返回结果。还有其他解决方案吗?在使用回调之前是否可以更改URL? – Alireza
你可以使用拦截器,但我会避免这种情况,并保持简单 – Blackbelt
你能否为我提供这种情况下的拦截器? – Alireza