【问题标题】:Arraylist from json and use it in recyclerView in Kotlin来自json的Arraylist并在Kotlin的recyclerView中使用它
【发布时间】:2020-05-06 18:53:52
【问题描述】:

我想在 Kotlin 中将 json 用于天气应用程序,特别是用于预测,我想将预测显示为列表...

我有这个来自 json 的数据类

data class X(
    val clouds: Clouds,
    val dt: Int,
    @SerializedName("dt_txt")
    val dtTxt: String,
    val main: Main,
    val snow: Snow,
    val sys: Sys,
    val weather: List<Weather>,
    val wind: Wind
) 

这是我的服务

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
private const val API_KEY = "de557725bd9504e26ff45267e4c3d806"

interface  ForcastServices{
    @GET("forecast")
    fun  getWeatherList(
        @retrofit2.http.Query("lat") lat :Double,
        @retrofit2.http.Query("lon") lon :Double
    ): Deferred<List<Weather>>


    companion object{

        operator fun invoke() :ForcastServices{
            val requestInterceptor = Interceptor{chain ->

                val url =chain.request()
                    .url()
                    .newBuilder()
                    .addQueryParameter("APPID", API_KEY)
                    .build()
                val request =chain.request()
                    .newBuilder()
                    .url(url)
                    .build()

                return@Interceptor chain.proceed(request)

            }

            val okHttpClient = OkHttpClient.Builder()
                .addInterceptor(requestInterceptor)
                .build()

            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://api.openweathermap.org/data/2.5/forecast?")
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ForcastServices::class.java)




        }



    }

}

这是我的 recyclerview 适配器

class RecyclerAdapterForcast(val weaTher: List<Weather>) :
    RecyclerView.Adapter<ForcastViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ForcastViewHolder {
        val fView =
            LayoutInflater.from(parent.context).inflate(R.layout.list_forcast, parent, false)
        return ForcastViewHolder(fView)
    }

    override fun getItemCount(): Int {
        return weaTher.size
        //return 8
    }

    override fun onBindViewHolder(holder: ForcastViewHolder, position: Int) {

        val weaTher = weaTher.get(position)
         holder.v.tex_test.text = weaTher.main





    }

}

这是我的 MainActivity

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val apiServices = ForcastServices()

    rec_forcast.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL ,false)
    GlobalScope.launch(Dispatchers.Main) {
        val currentForcastResponse = apiServices.getWeatherList(35.69,51.39).await()

            val weatherlist = currentForcastResponse
        rec_forcast.adapter = RecyclerAdapterForcast(weatherlist)
    }

但它有一些错误.. 这些是我在项目中看到的一些错误 有时会在当天预报中返回 有时会出现此错误 java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $

处为 BEGIN_OBJECT

任何人都有这个问题的解决方案...

【问题讨论】:

    标签: android json kotlin arraylist retrofit2


    【解决方案1】:

    您的 Data 类是 JSON OBJECT 的表示。错误说您的代码需要一个 JSON 数组。研究这两者之间的区别。还显示完整的错误消息帮助。

    【讨论】:

    • 我知道。但我找不到解决这个问题的解决方案.. json 链接在这里...[api.openweathermap.org/data/2.5/…
    • API链接没有key就不行,你的问题“太大了”...尽量消除API调用,只用字符串创建一个简单的问题,这样我们可以更容易看到真实问题?该错误表明 JSON 应该以“[”(BEGIN_ARRAY)而不是“{”(BEGIN_OBJECT)开头。似乎您的“天气预报”是一个对象,而不是一个数组?做一个listOf(weatherlist)也许可以解决问题?
    • 发布错误发生的代码行。您的错误消息应显示错误发生的位置。
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 2020-08-21
    • 2018-08-30
    • 2019-05-24
    相关资源
    最近更新 更多