简体   繁体   English

改造在字符串中转换我的字段 Int

[英]Retrofit transform my fields Int in String

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.但是当我做我的请求时,我的“field3”字段被作为字符串接收到服务器。

在此处输入图片说明

I do to not be converted to String ?我做的不是被转换为 String 吗?

Thank's谢谢

It is because of @FormUrlEncoded and @Field.这是因为@FormUrlEncoded 和@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:那么 JSON 结果将是:

{
    "name": "Robert",
    "verified_acc": "false",
    "points": "0",
    "id": 8
}

So my boolean and int values are now strings.所以我的 boolean 和 int 值现在是字符串。

But if you save it like this:但是如果你像这样保存它:

@POST("users")
Call<User> registerUser(@Body User user);

then the JSON result will be:那么 JSON 结果将是:

{
    "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.如果您对此没有问题,则可以在不将字段转换为字符串的情况下保存它。

I created an object like this:我创建了一个这样的对象:

data class UserLogin(val username: String, val password: String, val code: Int = 0)

And make it并使它

@POST("login")
fun login(@Body user: UserLogin

): Observable<String>

Put instead of int - long.把而不是 int - long。

Example:例子:

@Field("_auth_code") authCode: long = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM