I am currently trying to fetch a JSONArray from a server using Retrofit in Kotlin. Here is the interface I am using:
interface TripsService {
@GET("/coordsOfTrip{id}")
fun getTripCoord(
@Header("Authorization") token: String,
@Query("id") id: Int
): Deferred<JSONArray>
companion object{
operator fun invoke(
connectivityInterceptor: ConnectivityInterceptor
):TripsService{
val okHttpClient = OkHttpClient.Builder().addInterceptor(connectivityInterceptor).build()
return Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://someurl.com/")
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(TripsService::class.java)
}
}
}
the desired url is: https://someurl.com/coordsOfTrip?id=201
I am getting the following error message:
retrofit2.HttpException: HTTP 405 Method Not Allowed
I know the URL is working because I can access it via a browser.
Can someone please help me identify what I am doing wrong?
Just change the parameter from
@GET("/coordsOfTrip{id}")
to
@GET("/coordsOfTrip") // remove {id} part that's it
And you'd get the desired URL https://someurl.com/coordsOfTrip?id=201
If you want to use {id}
in GET()
then you've to use it like below
@GET("/coordsOfTrip{id}")
fun getTripCoord(
@Header("Authorization") token: String,
@Path("id") id: Int // use @Path() instead of @Query()
): Deferred<JSONArray>
But in your case it doesn't require. Follow the first method I mentioned.
For more check Retorfit's official documentation URL Manipulation part
Replace
@GET("/coordsOfTrip{id}")
with:
@GET("/coordsOfTrip?id={id}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.