我目前正在尝试使用 Kotlin 中的 Retrofit 从服务器获取 JSONArray。这是我正在使用的界面:
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)
}
}
}
所需的网址是:https://someurl.com/coordsOfTrip?id=201
我收到以下错误消息:
Retrofit2.HttpException:不允许 HTTP 405 方法
我知道该 URL 有效,因为我可以通过浏览器访问它。
有人可以帮我找出我做错了什么吗?
只需更改参数即可
@GET("/coordsOfTrip{id}")
to
@GET("/coordsOfTrip") // remove {id} part that's it
然后你就会得到想要的 URLhttps://someurl.com/coordsOfTrip?id=201
如果你想使用{id}
in GET()
那么你必须像下面这样使用它
@GET("/coordsOfTrip{id}")
fun getTripCoord(
@Header("Authorization") token: String,
@Path("id") id: Int // use @Path() instead of @Query()
): Deferred<JSONArray>
但在你的情况下不需要。按照我提到的第一种方法。
更多内容请查看 Retorfit 的官方文档URL操作 https://square.github.io/retrofit/#api-declaration part
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)