Android 应为BEGIN_对象,但在路径$[0]处为字符串

Android 应为BEGIN_对象,但在路径$[0]处为字符串,android,kotlin,retrofit2,moshi,Android,Kotlin,Retrofit2,Moshi,我尝试从链接解析JSON。这是我的密码 private const val BASE_URL = "https://raw.githubusercontent.com/" private val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() private val retrofit = Retrofit.Builder() .addConverterFactory

我尝试从链接解析JSON。这是我的密码

private const val BASE_URL = "https://raw.githubusercontent.com/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(RetrofitUtil.BASE_URL)
    .build()

interface DomainsApiService {
    @GET("Maximsiomin/DomainsAPI/master/domains_list.json")
    fun getAllDomains(): Call<List<Domain>>
}

object DomainsAPI {
    val retrofitService: DomainsApiService by lazy { retrofit.create(DomainsApiService::class.java) }
}

data class Domain(
    @Json(name = JSON.DOMAIN) val domain: String
)
private const val BASE\u URL=”https://raw.githubusercontent.com/"
private val moshi=moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val reformation=reformation.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(util.BASE\u URL)
.build()
接口域服务器{
@获取(“maxisiomin/DomainsAPI/master/domains\u list.json”)
fun getAllDomains():调用
}
对象域{
val ReformationService:DomainsApiService by lazy{Reformation.create(DomainsApiService::class.java)}
}
数据类域(
@Json(name=Json.DOMAIN)val-DOMAIN:String
)
这里我处理JSON:

fun getDomainsList(domain: String, context: Context) {
    DomainsAPI.retrofitService.getAllDomains().enqueue( object: Callback<List<Domain>> {
        override fun onFailure(call: Call<List<Domain>>, t: Throwable) {
            Timber.d("onFailure called")
            Toast.makeText(context, "Error: " + t.message, Toast.LENGTH_LONG).show()
        }

        override fun onResponse(call: Call<List<Domain>>, response: Response<List<Domain>>) {
            Timber.d("onResponse called")
            _response.value = response.body()?.size.toString()
        }
    })
}
fun getDomainsList(域:字符串,上下文:上下文){
DomainsAPI.ReturnationService.getAllDomains().enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
木材.d.(“称之为“不合格”)
Toast.makeText(上下文,“错误:+t.message,Toast.LENGTH\u LONG.show())
}
覆盖fun onResponse(调用:调用,响应:响应){
Timber.d(简称“onResponse”)
_response.value=response.body()?.size.toString()文件
}
})
}
但我遇到了Toast,错误为“预期的BEGIN_对象,但在路径$[0]处为字符串”。我试图编辑json文件,并做了一个dict,但得到了相同的错误。我能做什么?

改变

fun getAllDomains(): Call<List<Domain>>
fun getAllDomains():调用

fun getAllDomains():调用

因为api返回的是字符串列表而不是对象

您的api返回的是字符串数组,但您正在获取域对象数组

更改:

  Call<List<Domain>>
呼叫

呼叫

将起作用。

您的数据是字符串数组,而不是
对象数组。
  Call<List<Domain>>
  Call<List<String>>