android - java.lang.IllegalStateException : Expected BEGIN_OBJECT but was BEGIN_ARRAY Kotlin 错误

标签 android kotlin retrofit2

我正在尝试使用 Retrofit 在我的应用程序上实现登录,但是我不断收到此错误,不确定是什么问题,java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

这是 postman 的回复

  {
    "isSuccessful": true,
    "message": "successful",
    "user": [
        {
            "id": 1,
            "name": "Raymond Gitonga",
            "email": "xyz@gmail.com",
            "phone": "07222XXXXX"
        }
    ]
}

我有两个模型类用户模型类

   data class User(
    val id:Int,
    val name: String,
    val email:String,
    val phone:String
)

和登录响应类

  data class LoginResponse(
    val isSuccessful:Boolean,
    val message: String,
    val user:User
)

我的 Retrofit 对象

 object RetrofitClient {

    private const val BASE_URL = "http://10.0.2.2:7000/"

    val instance: RetrofitApi by lazy {
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    retrofit.create(RetrofitApi::class.java)
    }
}

改造接口(interface)

  interface RetrofitApi {
    @FormUrlEncoded
    @POST("users/login")
    fun userLogin(
        @Field("email") email:String,
        @Field("password")password:String
    ):Call<LoginResponse>
}

和我的登录类

        login_btn.setOnClickListener {
        val email = email_login.text.toString().trim()
        val password = password_login.text.toString().trim()

        if (email.isEmpty()){
            email_login.error = "Enter email"
            return@setOnClickListener
        }

        if (password.isEmpty()){
            password_login.error = "Enter password"
            return@setOnClickListener
        }

        RetrofitClient.instance.userLogin(email, password)
            .enqueue(object : Callback<LoginResponse> {
                override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                    Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()
                    println("YESSSSSSSSSSSSS>>>>>>"+t.message)
                }

                override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
                    if (response.body()?.isSuccessful!!){

                        SharedPreferenceManager.getInstance(applicationContext).saveUser(response.body()?.user!!)

                        val intent = Intent(applicationContext, MainActivity::class.java)
                        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

                        startActivity(intent)

                    }else{
                        Toast.makeText(applicationContext, response.body()?.message, Toast.LENGTH_LONG).show()
                    }
                }

            })
    }

最佳答案

val user:User

您的 JSON 显示用户列表,而不是单个用户。将上面的替换为:

val user: List<User>

关于android - java.lang.IllegalStateException : Expected BEGIN_OBJECT but was BEGIN_ARRAY Kotlin 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58358594/

相关文章:

android - Retrofit2+Kotlin 如何在获取响应作为数据类时查看原始 json

Android ActivityResult API Unresolved 引用错误 registerForActivityResult

Gradle - 只有在目录中的代码发生更改时才执行任务

android - 仅使用表单数据进行改造 2

Android:表示和渲染数学方程式

java - 可以在 Java 或 Kotlin 中以编程方式获取局部变量名吗?

android - MediaRecorder 捕获的音频文件在使用 Retrofit 2 发送到服务器后损坏

android - 如何解决 Android 10 的存储问题

Android:从最佳可用提供商处获取当前位置

android - Feed 广告中的 Facebook Native 在 RecyclerView 中相互重叠