I'm trying to use retrofit library. I have my fun :
@FormUrlEncoded
@POST("login")
fun login(@Field("field1") field1: String,
@Field("field2") field2: String,
@Field("field3") field3: Int = 0
): Observable<String>
and the definition of my retrofit object :
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("")
.client(get())
.build()
retrofit.create(RestApi::class.java)
.login(UserManager.username, UserManager.password, editextToken.text.toString().toInt())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
updateUiLogin()
val token = JwtManager.decodeTokenClaims(it)
UserManager.jwtToken = it
toast("Connexion réussis")
},
{ e ->
e as HttpException
updateUiLogin()
toast(R.string.an_error_occured)
Log.w(TAG, e.message())
}
)
but when i do my Request my "field3" Field is received as String to server.
I do to not be converted to String ?
Thank's
It is because of @FormUrlEncoded and @Field. If I save my user like this:
@POST("users")
@FormUrlEncoded
Call<User> registerFormUser(@Field("name") String name,
@Field("verified_acc") boolean verified_acc,
@Field("points") int points);
then the JSON result will be:
{
"name": "Robert",
"verified_acc": "false",
"points": "0",
"id": 8
}
So my boolean and int values are now strings.
But if you save it like this:
@POST("users")
Call<User> registerUser(@Body User user);
then the JSON result will be:
{
"id": 9,
"name": "George",
"points": 0,
"verified_acc": false
}
The only problem with the second aproach is that it changes the order alphabetically. If you have no problem with that, it's ok to save it without convert the fields to strings.