java.lang.IllegalStateException:应为BEGIN_OBJECT,但应为BEGIN_ARRAY Kotlin [英] java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY Kotlin

查看:143
本文介绍了java.lang.IllegalStateException:应为BEGIN_OBJECT,但应为BEGIN_ARRAY Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Retrofit在我的应用程序上实现登录,但是我一直收到此错误,不确定出什么问题了,java.lang.IllegalStateException:预期为BEGIN_OBJECT,但为BEGIN_ARRAY

I am trying to implement login on my app using Retrofit, i however keep getting this error not sure whats wrong, java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

这是邮递员的回复

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

我有两个模型类,即User模型类

I have two model classes the User model class

   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
)

我的改造对象

 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)
    }
}

Retrofit API

Retrofit api

  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显示用户列表,而不是单个用户.将上面的内容替换为:

Your JSON shows a list of users, not a single user. Replace the above with:

val user: List<User>

这篇关于java.lang.IllegalStateException:应为BEGIN_OBJECT,但应为BEGIN_ARRAY Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆