当我在Kotlin中转换为字符串时,归因数据类之一是否为空?

问题描述

我想将对象数据类转换为字符串,但是当我将其转换为字符串时,属性对象之一变为null,而当打印数据类的值时,该属性的值为0或1 这是代码

Json Response

@JsonIgnoreProperties(ignoreUnknown = true)
data class HistoryTreeResponseItem(
@field:JsonProperty("tree_id")
val id: Int,@field:JsonProperty("pemilik_lahan")
val landOwner: String,@field:JsonProperty("tree_number")
val treeNumber: String,@field:JsonProperty("nomor_anggota")
val membershipId: String,@field:JsonProperty("species_id")
val speciesId: Int,@field:JsonProperty("species_name")
val speciesName: String,@field:JsonProperty("diameter")
val diameter: Double,@field:JsonProperty("area_name")
val areaName: String,@field:JsonProperty("wood_product")
val woodProducts: List<HistoryProductResponseItem>)

@JsonIgnoreProperties(ignoreUnknown = true)
data class HistoryProductResponseItem(
@field:JsonProperty("wood_id")
val id: Int,@field:JsonProperty("tree_id")
val treeId: Int,@field:JsonProperty("log_status")
val logStatus: Int,@field:JsonProperty("harvest_order_id")
val orderId: Int,@field:JsonProperty("is_taken")
val isTaken: Int,@field:JsonProperty("log_sequence_number")
val logSequenceNumber: Int,@field:JsonProperty("log_number")
val logNumber: String,@field:JsonProperty("diameter")
val diameter: String,@field:JsonProperty("length")
val length: String,@field:JsonProperty("keliling")
val circumference: String,@field:JsonProperty("log_type")
val logType: Int,@field:JsonInclude(JsonInclude.Include.NON_NULL)
@field:JsonProperty("status_pecah")
val statusPecah: Int?,@field:JsonInclude(JsonInclude.Include.NON_NULL)
@field:JsonProperty("lubang_gerek")
val lubangGerek: Int?,@field:JsonInclude(JsonInclude.Include.NON_NULL)
@field:JsonProperty("pinhole")
val pinhole: Int?,@field:JsonInclude(JsonInclude.Include.NON_NULL)
@field:JsonProperty("belimbing")
val belimbing: Int?,@field:JsonProperty("created_by")
val createdBy: Int,@field:JsonProperty("deleted")
val deleted: Int,@field:JsonProperty("reject_status_petani")
val rejectStatusPetani: Int)

这是POJO类

data class HistoryTree(
val id: Int,val landOwner: String,val treeNumber: String,val membershipId: String,val speciesId: Int,val speciesName: String,val diameter: Double,val areaName: String,val woodProducts: List<HistoryProduct>)

data class HistoryProduct(
val id: Int,val treeId: Int,val logStatus: Int,val orderId: Int,val isTaken: Int,val logSequenceNumber: Int,val logNumber: String,val diameter: String,val length: String,val circumference: String,val logType: Int,val statusPecah: Int?,val lubangGerek: Int?,val pinhole: Int?,val belimbing: Int?,val createdBy: Int,val deleted: Int,val rejectStatusPetani: Int)

HistoryInteractor.kt

class HistoryInteractor(
val assignmentService: AssignmentService,val preferenceService: PreferenceService) {
suspend fun getHistory(): List<HistoryTree> {
    val accessId = preferenceService.accessId
    return assignmentService.getHistory(accessId)
}}

HistoryListActivity.kt

class HistoryListActivity : AppCompatActivity(),CoroutineScope {
private lateinit var job: Job
override val coroutineContext: CoroutineContext
    get() = Dispatchers.Main + job

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

private val vm: HistoryListViewModel by lazy { ViewModelProviders.of(this,viewModelFactory).get(HistoryListViewModel::class.java) }

private val mapper by lazy { jacksonObjectMapper() }

private var trees = mutableListOf<HistoryTree>()

private val treeAdapter = object : RecyclerView.Adapter<HistoryItemViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): HistoryItemViewHolder {
        val ui = HistoryItemUi()
        val view = ui.createView(AnkoContext.create(parent.context,parent))
        return HistoryItemViewHolder(ui,view)
    }

    override fun getItemCount(): Int = trees.size



    override fun onBindViewHolder(holder: HistoryItemViewHolder,position: Int) {
        holder.bind(trees[position]) {
            startActivity(intentFor<ProductHistoryListActivity>(
                ProductHistoryListActivity.paramTreeNumber to it.treeNumber,ProductHistoryListActivity.paramHistoryData to jacksonObjectMapper().writeValueAsString(it.woodProducts)
            ))
        }
    }
}

AssigmentService.kt

   override suspend fun getHistory(accessId: Int): List<HistoryTree> = suspendCancellableCoroutine { continuation ->
    harvestOrderApi.getHistory(accessId).enqueue(object : Callback<HistoryResponse> {
        override fun onFailure(call: Call<HistoryResponse>,t: Throwable) {
            continuation.resumeWithException(t)
        }

        override fun onResponse(call: Call<HistoryResponse>,response: Response<HistoryResponse>) {
            if (response.isSuccessful) {
                val result = response.body()
                if (result == null) continuation.resume(listOf()) else {
                    continuation.resume(result.data.map {
                        HistoryTree(
                            it.id,it.landOwner,it.treeNumber,it.membershipId,it.speciesId,it.speciesName,it.diameter,it.areaName,it.woodProducts.map { p ->
                                HistoryProduct(
                                    p.id,p.treeId,p.logStatus,p.orderId,p.isTaken,p.logSequenceNumber,p.logNumber,p.diameter,p.length,p.circumference,p.logType,p.statusPecah,p.lubangGerek,p.pinhole,p.belimbing,p.createdBy,p.deleted,p.rejectStatusPetani
                                )
                            }
                        )
                    })
                }
               
            } else {
                continuation.resumeWithException(Exception("failed get history(${response.code()})"))
            }
        }
    })
}

}

HistoryListViewModel.kt

class HistoryListViewModel @Inject constructor(
val historyInteractor: HistoryInteractor
) : ViewModel() {val historyData = MutableLiveData<List<HistoryTree>>()
suspend fun loadHistory() {
    val history = GlobalScope.async(Dispatchers.IO) {
        historyInteractor.getHistory()
    }.await()
    GlobalScope.launch(Dispatchers.Main) {
        historyData.value = history
        Log.d("data","history = $history")
    }
}}

我应该怎么做,因为属性值不能为空?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...