这是我要解析的JSON:-
{
"data": {
"relationships": {
"event": {
"links": {
"self": "/v1/feedbacks/1/relationships/event",
"related": "/v1/feedbacks/1/event"
}
},
"user": {
"links": {
"self": "/v1/feedbacks/1/relationships/user",
"related": "/v1/feedbacks/1/user"
}
}
},
"attributes": {
"rating": "4",
"comment": "Awesome event"
},
"type": "feedback",
"id": 1,
"links": {
"self": "/v1/feedbacks/1"
}
},
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "/v1/feedbacks/1"
}
}
我想从用户对象和自链接中获取用户名。 立即编写,仅能从atributes对象获得评分和评论, 以下是一些代码:-
Feedback.kt
@Type("feedback")
@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy::class)
@Entity
data class Feedback(
@Id(LongIdHandler::class)
@PrimaryKey
val id: Long? = null,
val rating: String?,
val comment: String?,
@Relationship("event")
var event: EventId? = null,
@Relationship("user")
var user: UserId? = null
)
FeedbackAPI.kt
interface FeedbackApi {
@GET("events/{eventId}/feedbacks?include=event")
fun getEventFeedback(
@Path("eventId") eventId: Long,
@Query("sort") sort: String = "rating",
@Query("filter") eventName: String = "[]"
): Single<List<Feedback>>
@POST("feedbacks")
fun postfeedback(@Body feedback: Feedback): Single<Feedback>
}
FeedbackDao.kt
@Dao
interface FeedbackDao {
@Insert(onConflict = REPLACE)
fun insertFeedback(feedbacks: List<Feedback>)
@Insert(onConflict = REPLACE)
fun insertSingleFeedback(feedback: Feedback)
@Query("SELECT * FROM feedback WHERE event = :eventId")
fun getAllFeedbackUnderEvent(eventId: Long): Single<List<Feedback>>
}
FeedbackViewHolder.kt
package org.fossasia.openevent.general.feedback
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_feedback.view.*
const val MAX_COMMENT_LINE = 3
class FeedbackViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(feedback: Feedback) {
itemView.commentTv.text = feedback.comment
itemView.ratingBar.rating = feedback.rating?.toFloat() ?: 0f
itemView.commentTv.setOnClickListener {
itemView.commentTv.maxLines =
if (itemView.commentTv.maxLines == MAX_COMMENT_LINE) Int.MAX_VALUE else MAX_COMMENT_LINE
}
}
}
FeedbackService.kt
class FeedbackService(
private val feedbackDao: FeedbackDao,
private val feedbackApi: FeedbackApi
) {
fun getFeedbackUnderEventFromDb(eventId: Long): Single<List<Feedback>> =
feedbackDao.getAllFeedbackUnderEvent(eventId)
fun getEventFeedback(id: Long): Single<List<Feedback>> =
feedbackApi.getEventFeedback(id).doOnSuccess {
feedbackDao.insertFeedback(it)
}
fun submitFeedback(feedback: Feedback): Single<Feedback> =
feedbackApi.postfeedback(feedback).doOnSuccess {
feedbackDao.insertSingleFeedback(it)
}
}
我该怎么做才能在android,restofit 2和json api中使用kotlin达到以下要求?