使用 Retrofit2 和 Kotlin 发送 JSON POST
Sending JSON POST using Retrofit2 and Kotlin
我正在尝试使用 Strawpoll's API 在 Kotlin 中使用 Retrofit2 创建投票。我无法从服务器获得正确的响应,即使 POST 似乎正常。这是我目前正在处理的内容。
IStrawpollDAO.kt
@Headers("Content-Type: application/json", "Accept:application/json")
@POST("polls")
fun createStrawpoll(@Body polls: Poll): Call<StrawpollPost>
Poll.kt
data class Poll(
@SerializedName("title") var title: String,
@SerializedName("options") var options: ArrayList<String>,
@SerializedName("multi") var multi: Boolean = true
)
StrawpollService.kt
fun createStrawpoll(): MutableLiveData<StrawpollPost>? {
var strawpoll = MutableLiveData<StrawpollPost>()
val service = StrawpollInstance.retrofitInstance?.create(IStrawpollDAO::class.java)
var options = ArrayList<String>()
options.add("Option 1")
options.add("Option 2")
var poll = Poll("31415926535897", options)
val call = service?.createStrawpoll(poll)
call?.enqueue(object : Callback<StrawpollPost> {
override fun onFailure(bigcall: Call<StrawpollPost>, t: Throwable) {
println("fail")
println(t.message)
println(bigcall.request().body)
}
override fun onResponse(call: Call<StrawpollPost>?, response: Response<StrawpollPost>?) {
if(response?.code() == 200) {
println("pass")
strawpoll.value = response?.body()
}
}
})
return strawpoll
}
StrawpollInstance.kt
object StrawpollInstance {
private var retrofit: Retrofit? = null
private val BASE_URL = "https://strawpoll.me/api/v2/"
val retrofitInstance: Retrofit?
get() {
if (retrofit == null) {
var httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
httpClient.interceptors().add(interceptor)
val gson = GsonBuilder()
.setLenient()
.create()
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build()
}
return retrofit
}
}
这里是来自 Logcat 的一些数据:
似乎响应目前只是返回他们的 HTML 错误页面,而不是返回预期的 JSON 对象。
我发了一个 POST 得到了回复:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://www.strawpoll.me/api/v2/polls">here</a>.</h2>
</body></html>
因此,将您的端点更改为 https://www.strawpoll.me/api/v2/polls。
我正在尝试使用 Strawpoll's API 在 Kotlin 中使用 Retrofit2 创建投票。我无法从服务器获得正确的响应,即使 POST 似乎正常。这是我目前正在处理的内容。
IStrawpollDAO.kt
@Headers("Content-Type: application/json", "Accept:application/json")
@POST("polls")
fun createStrawpoll(@Body polls: Poll): Call<StrawpollPost>
Poll.kt
data class Poll(
@SerializedName("title") var title: String,
@SerializedName("options") var options: ArrayList<String>,
@SerializedName("multi") var multi: Boolean = true
)
StrawpollService.kt
fun createStrawpoll(): MutableLiveData<StrawpollPost>? {
var strawpoll = MutableLiveData<StrawpollPost>()
val service = StrawpollInstance.retrofitInstance?.create(IStrawpollDAO::class.java)
var options = ArrayList<String>()
options.add("Option 1")
options.add("Option 2")
var poll = Poll("31415926535897", options)
val call = service?.createStrawpoll(poll)
call?.enqueue(object : Callback<StrawpollPost> {
override fun onFailure(bigcall: Call<StrawpollPost>, t: Throwable) {
println("fail")
println(t.message)
println(bigcall.request().body)
}
override fun onResponse(call: Call<StrawpollPost>?, response: Response<StrawpollPost>?) {
if(response?.code() == 200) {
println("pass")
strawpoll.value = response?.body()
}
}
})
return strawpoll
}
StrawpollInstance.kt
object StrawpollInstance {
private var retrofit: Retrofit? = null
private val BASE_URL = "https://strawpoll.me/api/v2/"
val retrofitInstance: Retrofit?
get() {
if (retrofit == null) {
var httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
httpClient.interceptors().add(interceptor)
val gson = GsonBuilder()
.setLenient()
.create()
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build()
}
return retrofit
}
}
这里是来自 Logcat 的一些数据:
似乎响应目前只是返回他们的 HTML 错误页面,而不是返回预期的 JSON 对象。
我发了一个 POST 得到了回复:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://www.strawpoll.me/api/v2/polls">here</a>.</h2>
</body></html>
因此,将您的端点更改为 https://www.strawpoll.me/api/v2/polls。