【发布时间】:2020-02-10 01:02:53
【问题描述】:
我正在尝试使用 Retrofit 在我的应用程序上实现用户注册,但是我一直收到此错误,不确定是什么问题,java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
这是邮递员的回复
{
"isSuccessful": true,
"message": "successful",
"user": {
"name": "Jackline Jazz",
"email": "jackijazz@gmail.com",
"phone": "000000"
}
}
我有两个模型类 User 模型类
data class User(
val name: String,
val email:String,
val phone:String
)
还有登录响应类
data class LoginResponse(
val isSuccessful:Boolean,
val message: String,
val user: List<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)
}
}
改造 api
@FormUrlEncoded
@POST("users/register")
fun userRegister(
@Field("name") name: String,
@Field("email") email: String,
@Field("password") password: String,
@Field("confirmPassword") confirmPassword: String
): Call<LoginResponse>
还有我的注册班
RetrofitClient.instance.userRegister(name, email, password, confirmPassword)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Toast.makeText(applicationContext, t.message, Toast.LENGTH_LONG).show()`
}
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
if (response.body()?.isSuccessful!!){
val intent = Intent(applicationContext, MainActivity::class.java)
startActivity(intent)
}else{
Toast.makeText(applicationContext, response.body()?.message, Toast.LENGTH_LONG).show()
}
}
})
}
}
如果可能的话,有人帮我实现 Kotlin 协程
【问题讨论】:
标签: android kotlin retrofit kotlin-coroutines