【发布时间】:2019-08-10 22:08:44
【问题描述】:
我正在使用 Dagger2 + Retrofit + RxAndroid + OkHttp3 + 新架构组件开发一个 android 应用程序。 最小 SDK = 16.
问题:在 API 16 上运行应用程序时,Url 生成不正确。 Url 缺少我通过 Retrofit 传递的 @QueryMap 参数。当我在 api 级别 21+ 上测试应用程序时,同样可以正常工作。
正确的网址 - 在 api 21+ - "http://api.apixu.com/v1/forecast.json?q=IDR&days=10&key=apikey"
在 api 16/19 上生成的网址 - “http://api.apixu.com/v1/forecast.json”
改造界面 -
@GET("forecast.json")
fun fetchWeatherDetails(
@QueryMap hashMap: @NotNull HashMap<String, String>
): @NotNull Observable<ApiResponse>
改造生成器 -
val httpClient = getOkHttpClient()
return Retrofit.Builder()
.baseUrl(apiBaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(httpClient)
.build()
OkHttpClient -
val builder = OkHttpClient.Builder()
builder
.cache(cache)
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.followRedirects(true)
.followSslRedirects(true)
val httpLoggingInterceptor = HttpLoggingInterceptor()
if (BuildConfig.DEBUG) {
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
} else {
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.NONE
}
builder.addInterceptor(HeaderInterceptor())
.addInterceptor(httpLoggingInterceptor)
return builder.build()
自从我陷入这个问题以来已经超过 2 天了。任何帮助将不胜感激。
更新: API 查询代码在 API 21+ 上运行良好。
在 API-16 和 API-19 上失败。
【问题讨论】:
-
您的问题很奇怪,只能说一个建议,尝试使用查询而不是 QueryMap。可以解决您的问题。
-
试过了。使用@Query - 同样的问题。当传递整个网址时 - 它工作正常。我认为这个问题很简单——在 api23+ 上解析 url,但在 api 16 上没有解析。我想知道它是否与 api 级别 16 的向后兼容性有关。
标签: android retrofit rx-java okhttp3 okhttp