2017-11-02 5 views
1

기본적으로이 작업을 수행하려고 시도합니다. 이 모든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> 

오류가 이메일로 간다 : 문자열 내가 요청 이런 종류의 가장 좋은 방법은이 값으로 클래스를 만들 수 있다고 알고 요청 및 JSON에서 얻은 그러나 나는

가이

의 구조 내 Person.kt에 같은 값 이메일을 작성하고 싶지 않아 16,

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이 실패 할 때 throw하는 새로운 콘텐츠입니다.

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 $

답변

0

호출에는 1 개의 body 매개 변수가있을 수 있습니다.
에만 1.

"사용자"클래스를 확장하는 "RequestUser"와 같은 새 클래스를 만든 다음 해당 클래스에 "전자 메일"필드를 추가해야합니다.

앱에서 "사용자"클래스를 사용하지만 요청을하려면 "사용자"개체의 모든 데이터를 복사하는 새로운 "RequestUser"개체를 만들고 요청에 전자 메일을 추가하십시오 "RequestUser"개체를 사용하여 요청을 수행 할 수 있습니다.

또한 영어로 코드를 작성하십시오. 코드의 절반이 다른 언어로되어있을 때 코드를 읽을 수 없습니다.

+0

잘 영어로 코드를 변경 (내 나쁜), 내가 클래스 사용자를 확장 할 때, 나는 JSON을 내가 시각적으로 보내고받는 것을 시각화 할 수 없다. –