Android 第2步向Body对象添加额外字符串
基本上我想做的就是这个 首先,这是我的APIService.ktAndroid 第2步向Body对象添加额外字符串,android,kotlin,retrofit,retrofit2,Android,Kotlin,Retrofit,Retrofit2,基本上我想做的就是这个 首先,这是我的APIService.kt @Headers("Content-Type: application/json") @POST("api/v1/UsuarioController/saveDataPerson") abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person> class Person : Serializabl
@Headers("Content-Type: application/json")
@POST("api/v1/UsuarioController/saveDataPerson")
abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person>
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
}
}
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;
}
}
这是我调用email的另一个类(用于登录、注册)
User.kt
@Headers("Content-Type: application/json")
@POST("api/v1/UsuarioController/saveDataPerson")
abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person>
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
}
}
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()
}
})
}
private fun register personaldata(){
btnBMI.isClickable=false
btnBMI.startAnimation()
val person=restManager!!.getApiService()!!.saveDataPerson(person(User(“rol”,commonMethod.getUserPreferenceValue(),“password”,true),commonMethod.categoryBMI(commonMethod.calcHeightMeters)(et_height.text.toString().toFloat(),heightRadioSelect.position.toString()),commonMethod.calcweightkg(et_weight.text.toString()).toFloat(),weights radioselect.position.toString().toInt()),et_name.text.toString(),genderRadioSelect.position.toString().toBoolean(),age.toInt(),et_height.text.toString().toFloat(),et_weight.text.toString().toFloat())
排队(对象:回调{
覆盖fun onResponse(调用:调用,响应:2.响应){
何时(response.body()?.message){
“空字段”->Toast.makeText(this@RegisterBMIActivity,getString(R.string.invalid_data),Toast.LENGTH_LONG.show()
“无链接帐户”->Toast.makeText(this@RegisterBMIActivity,getString(R.string.no\u关联的\u帐户),Toast.LENGTH\u LONG.show()
“无IMC链接”->Toast.makeText(this@RegisterBMIActivity,getString(R.string.bmi_未知),Toast.LENGTH_LONG.show()
“无效数据”->Toast.makeText(this@RegisterBMIActivity,getString(R.string.invalid_data),Toast.LENGTH_LONG.show()
“成功”->registroExitoso()
else->Toast.makeText(this@RegisterBMIActivity,getString(R.string.unknown\u错误),Toast.LENGTH\u LONG.show()
}
btnBMI.isClickable=true
btnBMI.doneloadinganization(R.color.md_yellow_300,(BitmapFactory.decodeResource(resources,R.drawable.check)))
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.e(“错误”,t.message)
btnBMI.isClickable=true
btnBMI.revertAnimation()
}
})
}
我想实现的目标是在@Body中添加电子邮件,而不将该字段添加到Person.kt
@Headers("Content-Type: application/json")
@POST("api/v1/UsuarioController/saveDataPerson")
abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person>
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
}
}
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;
}
}
请告诉我:(
[[编辑]]
这是JSON失败时抛出的新内容
E/错误:java.lang.IllegalStateException:应为BEGIN\u对象,但
was字符串位于第2行第1列路径$
E/errors:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:
应为BEGIN_对象,但在第2行第1列路径处为字符串$
一个调用可以有一个主体参数。
只有一个 因此,您必须创建一个新类,比如扩展“User”类的“RequestUser”,然后在该类中添加“email”字段 因此,您在应用程序中使用“User”类,但当您想要发出请求时,您可以创建一个新的“RequestUser”对象,该对象复制“User”对象中的所有数据,并将电子邮件添加到“RequestUser”对象中,并使用该对象执行请求
另外,请用英语编写代码。如果代码有一半是用其他语言编写的,则无法读取代码。问题是,当您调用要分配的对象时,它与您收到的当前结构不匹配。我将代码改为英语(我的坏代码)此外,当我扩展类用户时,我得到了一个格式错误的异常,但是我无法可视化我发送和接收的JSON:(