拥有一个生成 json 作为响应的主机。
http://example.com/get-json/getJsonPosts.php?2
其中 2 是帖子 ID。
我无法@Get传递参数。
interface Api {
@GET("getJsonPosts.php?{id}")
fun loadtPost(@Path("id") groupId: Int): Observable<ArrayList<Card>>
}
执行
override fun uploadData() {
var api : Api? = null
api = MyApplication().connectServer()
api!!.loadtPost(10) // Передаю параметр
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ e ->
recyclerView.adapter!!.notifyItemInserted(cards.size - 1)
for (i in 0 until e.size) {
cards.add(Card(e[i].photoUrl,
e[i].title,
e[i].date,
e[i].description,
e[i].photoUrl,
e[i].likeCaunt))
}
})
}
我收到一个错误
java.lang.IllegalArgumentException: URL query string "{id}" must not have replace block. For dynamic query parameters use @Query.
我现在正在尝试这个
API {
@GET("getJsonPosts.php?")
fun loadtPost(@Query("") groupId: Int): Observable<ArrayList<Card>>
}
我收到一个错误
io.reactivex.exceptions.OnErrorNotImplementedException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
也许我需要像这样实现请求:
http://example.com/get-json/getJsonPosts.php?id=2
编辑后 06/06/18
原来,有必要像这样在服务器上接受一个 url 请求。
之前,我在服务器上处理了这个url来获取json。
现在
@Get您可以用这种方式替换动态参数。以前,我的做法有所不同,请参阅提出的问题。这个怎么运作
@GET 注释不仅会发送一个 GET 请求,它还
.baseUrl会将getJsonPosts.php添加到它。对生成的 @GET 请求本身添加注释 @Query ?以及由 @Query("id <- here ")指定的单词id并将替换最后的符号= 2,其中 2 是传递的参数。结果http://example.com/get-json/getJsonPosts.php?id=2
基本网址
}
了解更多
网站本身没有关于使用注释的详细信息。要查看特定注释的工作方式,请将鼠标悬停在注释上,单击并按 F4 (Windows)。您将打开源代码,在评论中您会发现它是如何工作的以及它可以生成什么 url。
如果我帮助了你,请单击向上箭头