Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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

This is the response from postman

  {
    "isSuccessful": true,
    "message": "successful",
    "user": [
        {
            "id": 1,
            "name": "Raymond Gitonga",
            "email": "[email protected]",
            "phone": "07222XXXXX"
        }
    ]
}

I have two model classes the User model class

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

And the login response class

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

my Retrofit object

 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

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

and my login class

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

            })
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
val user:User

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

val user: List<User>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...