【发布时间】:2017-11-02 18:32:36
【问题描述】:
基本上我想做的就是这个。
首先这是我的APIService.kt
@Headers("Content-Type: application/json")
@POST("api/v1/UsuarioController/saveDataPerson")
abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person>
错误与 email: String 有关在我的 Person.kt
中创建相同价值的电子邮件结构是这样的
Person.kt
class Person : Serializable {
var user: User
var imc: String
var name: String
var gender: Boolean
var age: Int
var height: Float
var weight: Float
@SerializedName("message")
var message: String? = null
constructor(user: User,imc: String, name: String, gender: Boolean, age: Int, height: Float, weight: Float){
this.user = User
this.imc = imc
this.name = name
this.gender = gender
this.age = age
this.height = height
this.weight= weight
}
}
这是我调用电子邮件的另一个类(用于登录、注册)
User.kt
class Usuario() : Serializable {
var rol: String = ""
var email: String = ""
var pass: String = ""
var active: Boolean = false
@SerializedName("message")
var message: String? = null
constructor(rol: String, email: String, pass: String, active: Boolean): this(){
this.rol = rol;
this.email = email;
this.pass = pass;
this.active = active;
}
}
以及执行此操作的类。
private fun registerPersonalData(){
btnBMI.isClickable = false
btnBMI.startAnimation()
val person = restManager!!.getApiService()!!.saveDataPerson(Person(User("rol",commonMethod.getUserPreferenceValue(),"password",true),commonMethod.categoryBMI(commonMethod.calcBMI(commonMethod.calcHeightMeters(et_height.text.toString().toFloat(),heightRadioSelect.position.toString().toInt()),commonMethod.calcWeightKilograms(et_weight.text.toString().toFloat(),weightRadioSelect.position.toString().toInt()))),et_name.text.toString(),genderRadioSelect.position.toString().toBoolean(),age.toInt(),et_height.text.toString().toFloat(),et_weight.text.toString().toFloat()))
person.enqueue(object : Callback<Person> {
override fun onResponse(call: Call<Person>, response: retrofit2.Response<Person>) {
when(response.body()?.message){
"Empty Fields"->Toast.makeText(this@RegisterBMIActivity,getString(R.string.invalid_data),Toast.LENGTH_LONG).show()
"No linked account"->Toast.makeText(this@RegisterBMIActivity,getString(R.string.no_associated_account),Toast.LENGTH_LONG).show()
"No IMC linked"->Toast.makeText(this@RegisterBMIActivity,getString(R.string.bmi_unknown),Toast.LENGTH_LONG).show()
"Invalid data"-> Toast.makeText(this@RegisterBMIActivity,getString(R.string.invalid_data),Toast.LENGTH_LONG).show()
"Success"->registroExitoso()
else -> Toast.makeText(this@RegisterBMIActivity,getString(R.string.unknown_error),Toast.LENGTH_LONG).show()
}
btnBMI.isClickable = true
btnBMI.doneLoadingAnimation(R.color.md_yellow_300, (BitmapFactory.decodeResource(resources,R.drawable.check)))
}
override fun onFailure(call: Call<Person>, t: Throwable) {
Log.e("error", t.message)
btnBMI.isClickable = true
btnBMI.revertAnimation()
}
})
}
我想要实现的目标是将 email 添加到我的@Body 中,而不将该字段添加到 Person.kt
请给我建议:(
[[编辑]]
这是 JSON Fail 时抛出的新内容...
E/error: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但 是 STRING 在第 2 行第 1 列路径 $
E/errors:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_OBJECT,但在第 2 行第 1 列路径 $
【问题讨论】:
标签: android kotlin retrofit retrofit2