我是 kotlin 的新手,我正處于學習階段。我遵循了許多鏈接,但無法完全理解。我希望 Json 回應顯示在我的文本視圖中。
問題:1 我已經嘗試過這段代碼,但無法獲取資料,但我想獲取資料物件中的專案。參考和作者將變為空。
{
"status": 200,
"message": "Success",
"data": {
"Quote": "The pain you feel today will be the strength you feel tomorrow.",
"Author": ""
},
"time": "0.14 s"
}
問題:2 我不知道如何在 textview 中決議此回應
object ServiceBuilder {
private val client = OkHttpClient.Builder().build()
private val retrofit = Retrofit.Builder()
.baseUrl("https://url.com.pk/") // change this IP for testing by your actual machine IP
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
fun<T> buildService(service: Class<T>): T{
return retrofit.create(service)
}}
RESTAPI
interface RestApi{
@Headers("Content-Type: application/json")
@POST("api/getquotes")
abstract fun addUser(@Body userData: UserInfo): Call<UserInfo>}
RESTAPI服務
class RestApiService
{
fun addUser(userData: UserInfo, onResult: (UserInfo?) -> Unit)
{
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<UserInfo>
{
override fun onFailure(call: Call<UserInfo>, t: Throwable)
{
onResult(null)
}
override fun onResponse( call: Call<UserInfo>, response: Response<UserInfo>)
{
val addedUser = response.body()
Log.d("responsee","" addedUser)
onResult(addedUser)
}
}
)
}
}
用戶資訊
data class UserInfo (
@SerializedName("Quote")
val quote : String,
@SerializedName("Author")
val author : String
)
主要活動
fun getQuotes() {
val apiService = RestApiService()
val userInfo = UserInfo("","")
apiService.addUser(userInfo) {
Log.d("Error registering user","errter")
/*if ( != null)
{
// it = newly added user parsed as response
// it?.id = newly added user ID
} else {
Log.d("Error registering user","errter")
}*/
}
}
任何幫助,將不勝感激 :)
uj5u.com熱心網友回復:
狀態、訊息和資料都是回應的一部分,因此您需要注意這些。例如這個
data class AddUserResponse(
val `data`: UserInfo, //like you defined it
val message: String,
val status: Int,
val time: String
)
這意味著引數和回應不同,因此需要將 RestApi 更改為此
abstract fun addUser(@Body userData: UserInfo): Call<AddUserResponse>}
這反過來也改變了服務中的型別,比如
class RestApiService
{
fun addUser(userData: UserInfo, onResult: (UserInfo?) -> Unit)
{
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<AddUserResponse>
{
override fun onFailure(call: Call<AddUserResponse>, t: Throwable)
{
onResult(null)
}
override fun onResponse( call: Call<AddUserResponse>, response: Response<AddUserResponse>)
{
val addedUser = response.body()
Log.d("responsee","" addedUser)
onResult(addedUser.data)
}
}
)
}
}
現在在 getQuotes 中,您將擁有it一個 UserInfo 物件
apiService.addUser(userInfo) {
val returnedUserInfo = it
}
uj5u.com熱心網友回復:
只需按照我的步驟:
File->settings->Plugins
搜索JSON To Kotlin class并安裝它
再次點擊 File->New->Kotlin Data class from JSON
將您的 json 代碼粘貼到此處,然后單擊生成。它將生成 POJO 類,您就可以開始了。
uj5u.com熱心網友回復:
我注意到的第一件事是,您的 json 中的資料是:
"Quote": "The pain you feel today will be the strength you feel tomorrow.",
"Author": ""
當你UserInfo定義 @SerializedName("message")了Quote。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/366628.html
下一篇:無效的狀態機定義
