基本的に何をしようとしているのですか?このすべてのRetrofit2 Bodyオブジェクトに余分な文字列を追加
まず、私のAPIService.ktある
@Headers("Content-Type: application/json")
@POST("api/v1/UsuarioController/saveDataPerson")
abstract fun saveDataPerson(@Body email: String,@Body Person: Person): Call<Person>
エラーがメールで行く:文字列 iは、要求のこの種のために最善の方法は、それが値を持つクラスを作成することだということを知っています要求されたとJSONから得た、しかし、私は私の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([email protected],getString(R.string.invalid_data),Toast.LENGTH_LONG).show()
"No linked account"->Toast.makeText([email protected],getString(R.string.no_associated_account),Toast.LENGTH_LONG).show()
"No IMC linked"->Toast.makeText([email protected],getString(R.string.bmi_unknown),Toast.LENGTH_LONG).show()
"Invalid data"-> Toast.makeText([email protected],getString(R.string.invalid_data),Toast.LENGTH_LONG).show()
"Success"->registroExitoso()
else -> Toast.makeText([email protected],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()
}
})
}
、よく私はそれがPerson.kt
にそのフィールドを追加することなく、私の@Bodyにメールを追加することです達成したい目標は、私にアドバイスをしてください:(
を[[編集済み]]
これは、JSONが失敗したときにスローする新しいコンテンツです。
E/error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
E/errors:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 1 path $
私はコードを英語に変更しています(私の悪い)また、私はクラスのユーザーを拡張すると、私はJSONを私が送信して受信することはできません –