我正在为我的网络层使用改造我面临一个奇怪的问题我已经参数了,它的值看起来像这样
店:// 0TxIGQMQbObzvU4Apia0V0&安培;回调=
并在编码后更改为此
店:// 0TxIGQMQbObzvU4Apia0V0%26callback%3D
由于某种原因,服务器不喜欢这种编码,而且我得到了#400; HTTP 400 Bad Request"。
如果我在没有编码的情况下点击它运行正常,那么我想知道是否有任何方法可以禁用内容。
我尝试过其他类似的方法
@Query(value "env" encoded = false) String env
但没有运气。
这是我的网络接口类
public interface NetworkService
{
@GET("v1/public/yql")
Observable<SeriesEntity> getOnGoingSeries(@Query("q") String query,
@Query("format") String format,
@Query("diagnostics") boolean flag,
@Query("env") String env);
class Factory {
public static NetworkService create()
{
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://query.yahooapis.com")
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit.create(NetworkService.class);
}
}
}
修改:
我还注意到问题只是&#39;&amp;&#39;的编码值。因为如果我替换它的值(%是%26)它可以正常工作
答案 0 :(得分:2)
我认为这个问题是你将callback
param作为env
一部分的一部分。尝试将@Query("callback") boolean callback
添加到您的改造界面(store://0TxIGQMQbObzvU4Apia0V0
只使用env
)
答案 1 :(得分:1)
请使用 encoded = true
@Query(值=“电子邮件”,编码= true)
@HTTP(method =“ DELETE”,path = ApiConstant.DELETE_CONTACT_INFO,hasBody = true)
可观察到的deleteContactInfo(@Query(value =“ email”,encoding = true)字符串电子邮件);
答案 2 :(得分:0)
Github issue 1199的更好解决方案,我们可以使用自定义拦截器是解决此问题的一种方法。这个解决方案对我有用。
根据您的情况,我想将%26替换为url中的=。
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String string = request.url().toString();
string = string.replace("%26", "=");
Request newRequest = new Request.Builder()
.url(string)
.build();
return chain.proceed(newRequest);
}
});
答案 3 :(得分:0)
URL为:
https://api.themoviedb.org/3/discover/movie?api_key={"PUTAPIKEYHERE!!!}&with_genres=28
我得到了:
https://api.themoviedb.org/3/discover/movie?api_key={"PUTAPIKEYHERE!!!}&with_genres%3D=28
fun getClient(codeForActionOrComedy:Int): MovieDbInterfaceToGetMovies {
val requestToApiInterceptor=Interceptor { chain ->
val url =chain.request()
.url()
.newBuilder()
// ALONE_API is just the "PUTAPIKEYHERE!!!"
.addQueryParameter("api_key","PUTAPIKEYHERE!!!")
//codeForActionOrComedy is an Int
.addQueryParameter("with_genres","$codeForActionOrComedy")
.build()
val request =chain.request()
.newBuilder()
.url(url.toString().replace("%3D","="))
.build()
return@Interceptor chain.proceed(request)
}
val okHttpClient= OkHttpClient.Builder()
.addInterceptor(requestToApiInterceptor)
.connectTimeout(60, TimeUnit.SECONDS)
.build()
val httpUrl = Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MovieDbInterfaceToGetMovies::class.java)
return httpUrl
}