![](/img/trans.png)
[英]java.lang.IllegalArgumentException: baseUrl must end in / while using retrofit 2.1.0 for GET method
[英]baseurl must end in /
我有天氣 api 來解析數據 10 天
一切都很好,但我現在遇到 retrofit 問題,我的應用程序崩潰了,我的 URL(帶有 API)最后有 /。
但還是不行。
我也有 retrofit 的依賴注入。
目標是從api獲取數據。
希望您能幫助我解決這個問題。
package const
const val BASE_URL = "https://api.weatherapi.com/v1/forecast" +
".json?key=a9f9d57b6e064f16b28141346231001&q=London&days=10&aqi=no&alerts=no/" // error here
const val apikey = "a9f9d57b6e064f16b28141346231001"
const val WeatherDays = 10
interface WeatherServiceAPI {
@GET("forecast.json")
suspend fun Weatherday(
@Query("days") days : Int
) : WeatherResponse
@GET("forecast.json")
suspend fun searchcitybycoord(@Query("lat")lat:String) : List<WeatherLocationDTO>
@GET("forecast.json")
suspend fun searchingbyCity(@Query("q") name: String) : List<WeatherLocationDTO>
companion object{
operator fun invoke(
connectivityInterceptor: Interceptor
):WeatherServiceAPI{
val requestInterceptor = Interceptor{
chain -> val url = chain.request()
.url
.newBuilder()
.addQueryParameter("key", apikey)
.build()
val request = chain.request()
.newBuilder()
.url(url)
.build()
return@Interceptor chain.proceed(request)
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(requestInterceptor)
.addInterceptor(connectivityInterceptor)
.build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://api.weatherapi.com/v1/") // error line
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherServiceAPI::class.java)
}
}
}
@Provides
@Singleton
fun providerepository(api:WeatherServiceAPI):ForecastRepository{
return ForecastRepositoryImpl(api)
}
@Provides
@Singleton
fun provideWeatherApiService(retrofit: Retrofit) =
retrofit.create(WeatherServiceAPI::class.java)
@Provides
@Singleton
fun provideRetrofit ( okHttpClient: OkHttpClient) = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.client(okHttpClient)
.build()
@Provides
@Singleton
fun provideOkhttpClient(interceptor: Interceptor): OkHttpClient {
val httpBuilder = OkHttpClient.Builder().addInterceptor(interceptor)
return httpBuilder.build()
}
@Provides
@Singleton
fun provideinterceptor():Interceptor{
return Interceptor {
val request =it.request().newBuilder()
val actualRequest = request.build()
it.proceed(actualRequest)
}
}
鑒於您在代碼中還有其他內容,您的基礎 URL 應該是https://api.weatherapi.com/v1/
。
forecast.json
來自@GET
注釋,查詢參數將需要來自@Query
參數到您的 Retrofit 接口函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.