Kotlin okhttp解析错误以回收视图



我有一个json文件,其中包含一些数据。我试图使用模态类将这些数据解析为数组中的kotlin,并在回收器视图中显示,但无法获取,在我启动活动时,应用程序不断崩溃
我想要解析的Json数据

成员银行API:[MemberBankModel(bankName=联盟银行,memberBankAccNumber=11111111(,MemberBankModel(bankName=Bank Simpanan Nasional,memberbankAccNumer=2222222222(]
从Log.d记录("成员银行API","${saveBankResponseModel.data!!.memberBank}"(

Activity.kt

okHttpClient.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
if(response.isSuccessful) {
saveBankResponseModel = json.decodeFromString(
response.body!!.string()
)
Log.d("Member Bank API","${saveBankResponseModel.data!!.memberBank}")
val noBankView = findViewById<LinearLayout>(R.id.nobank_layout)
newRecyclerView = findViewById(R.id.recyclerView)
noBankView.visibility = View.GONE
newRecyclerView.apply {
layoutManager = LinearLayoutManager(this@MyWalletActivity)
adapter = saved_bank_adapter(saveBankResponseModel.data!!.memberBank)
}
}
}
override fun onFailure(call: Call, e: IOException) {
mHandler.post {
println(e)
}
}
})

数据类

@Serializable
data class SaveBankResponseModel(
val responseCode:Int,
val msgType:String,
val message:List<String>,
val data:SaveBankDataModel? = null
)
@Serializable
data class SaveBankDataModel(
val accountHolder:AccountModel,
val memberBank:List<MemberBankModel>
)
@Serializable
data class AccountModel(
val name:String,
)
@Serializable
data class MemberBankModel(
val bankName:String,
val memberBankAccNumber:String
)

再生适配器

class saved_bank_adapter(private val bankList: List<MemberBankModel>): RecyclerView.Adapter<saved_bank_adapter.BankViewHolder>() {
private var selectedItemPosition: Int = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BankViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.withdrawbank_layout, parent, false)
return BankViewHolder(itemView)
}
override fun onBindViewHolder(holder: BankViewHolder, @SuppressLint("RecyclerView") position: Int) {
val currentItem = bankList[position]
holder.itemName.text = currentItem.bankName
holder.itemAccNum.text = currentItem.memberBankAccNumber
}

有人能帮我检查一下我做错了什么步骤吗?我是新来的Kotlin API调用

我得到错误。。。

2022-08-22 16:44:06.584 8526-8632/com.example.app E/AndroidRuntime:致命异常:OkHttp Dispatcher流程:com.example.app,PID:8526android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能访问其视图。

正如我所看到的,memberBankAccNumber字段值是API响应中的一个整数,但您已经将该构建声明为String,因此错误就在这里。

val memberBankAccNumber:String

将其替换为

val memberBankAccNumber:Int

相关内容

  • 没有找到相关文章

最新更新