我正在尝试创建POST请求,以使用JSON中的电子邮件和密码参数登录用户。
我遇到以下错误:
AuthService.kt
interface AuthService {
@POST("/user/signin")
fun login(@Body request: JSONObject) : Call<PostLoginResponse>
}
PostLoginResponse.kt
data class PostLoginResponse(
val access_token: String,
val expires_in: Number,
val token_type: String
)
LoginActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login)
email = findViewById(R.id.input_email)
password = findViewById(R.id.input_password)
signinButton = findViewById(R.id.btn_login)
signinButton.setOnClickListener {
val authJsonData = JSONObject()
authJsonData.put("email", email.text.toString().trim())
authJsonData.put("password", password.text.toString().trim())
login(authJsonData);
}
}
private fun login(jsonData: JSONObject) {
val call = App.authService.login(jsonData)
call.enqueue(object : Callback<PostLoginResponse> {
override fun onResponse(call: Call<PostLoginResponse>, response: Response<PostLoginResponse>) {
Log.i(TAG, "login() - onResponse() Result = ${response?.body()}")
}
override fun onFailure(call: Call<GetSitesResponse>, t: Throwable) {
Log.e(TAG, "login() - onFailure() ", t)
}
})
}
答案 0 :(得分:2)
通过call
方法将Call<GetSitesResponse>
参数类型从Call<PostLoginResponse>
更改为onFailure
:
override fun onFailure(call: Call<PostLoginResponse>, t: Throwable) {
Log.e(TAG, "login() - onFailure() ", t)
}