改造后数据为空

问题描述 投票:0回答:1

我正在编写货币应用程序。 收到空数据(Json正常)。 我尝试调试它,但我不明白错误在哪里以及是什么。 如果您能帮助我了解错误所在,我将不胜感激。 这是我的代码:

API服务:

interface ApiService {

    @GET("daily_json.js")
    suspend fun getValute(): Response<Valute>
}

改造实例:

object RetrofitInstance {

    private val retrofit by lazy {
        Retrofit.Builder().baseUrl("https://www.cbr-xml-daily.ru/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
    val api: ApiService by lazy {
        retrofit.create(ApiService::class.java)
    }
}

存储库:

class Repository {

    suspend fun getVal(): Response<Valute>{
        return RetrofitInstance.api.getValute()
    }

}

视图模型:

class StartViewModel: ViewModel() {

    var repo = Repository()
    val moneyList: MutableLiveData<Response<Valute>> = MutableLiveData()


    fun getMoney(){
        viewModelScope.launch {
            moneyList.value = repo.getVal()
        }
    }
}

片段:

       class StartFragment : Fragment() {
        private lateinit var _binding: FragmentStartBinding
        private val binding get() = _binding!!
        private val adapter = StartAdapter()
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            _binding = FragmentStartBinding.inflate(inflater, container, false)
            val view = binding.root
    
    
            return view
    
        }
    
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            binding.rvStart.adapter = adapter
            val viewModel = ViewModelProvider(this).get(StartViewModel::class.java)
            viewModel.getMoney()
            viewModel.moneyList.observe(viewLifecycleOwner,{list ->
                list.body()?.let { adapter.setList(it) }
            })
}

适配器:

class StartAdapter: RecyclerView.Adapter<StartAdapter.StartHolder>() {

    var listValute = emptyList<Valute>()

class StartHolder(item: View): RecyclerView.ViewHolder(item) {

    val binding = ItemBinding.bind(item)

    fun bind(valute: Valute) = with(binding){

        nameMoney.text = valute.CharCode
        purchase.text = valute.Value.toString()

    }
}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StartHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
        return StartHolder(view)
    }

    override fun getItemCount(): Int {
        return listValute.size
    }

    override fun onBindViewHolder(holder: StartHolder, position: Int) {
        holder.bind(listValute[position])
    }

    fun setList(valute: Valute){
        listValute = listOf(valute)
        notifyDataSetChanged()
    }
}

我附上了您可能需要的所有代码。

更新 我忘记添加 DataClass

data class Valute(
    val CharCode: String,
    val ID: String,
    val Name: String,
    val Nominal: Int,
    val NumCode: String,
    val Previous: Double,
    val Value: Double
): Serializable
android kotlin retrofit
1个回答
0
投票

您的数据模型与 API 返回的数据不匹配。 您的

Valute
(数据)类是地图的一个元素(位于键“Valute”下。

因此,首先为答案创建一个通用模型,例如如下所示:

data class ValuteResponse(
    val date: String,
    val previousDate: String,
    val previousURL: String,
    val timestamp: String,
    val valute: Map<String, Valute>,
)

然后使用它(在

ApiService
中)作为 API 的响应模型:

@GET("daily_json.js")
suspend fun getValute(): Response<ValuteResponse>

有许多网站可以从现有的 JSON 为您创建类:

© www.soinside.com 2019 - 2024. All rights reserved.