1

I need to send a large data class over Http request. Here is my class:

data class User(
    val uid: String,
    val mobNum: String,
    val isValidated: Boolean,
    val isActivated: Boolean,
    val gender: String,
   ...
   ...
   ...)

Here is the Api service I use for updating a part of this data. If needed.

internal interface ProfileApi {    
    @POST("update/profile")
    @FormUrlEncoded
    fun updatePending(
        @Field("_id") uid: String,
        @Field("mobile_num")  mobNum: String,
        @Field("is_validated") isValidated: Boolean,
        @Field("is_activated") isActivated: Boolean,
        @Field("gender") gender: String,
        .....
        ....
    ): Call<ResponseClass>
}

But this time I need to send request of all 50 or more fields. How can I map data-keys to field-keys and corresponding value with @FieldMap or any other method. thanks.

binrebin
  • 357
  • 4
  • 16

2 Answers2

1

Here is the work around.. Create a new class: refer this

data class UserMap(
    val _id: String,
    val mobile_num: String,
    val is_validated: Boolean,
    val is_activated: Boolean,
    val gender: String
){
    object ModelMapper {
        fun from(user: User) =
            UserMap(user.uid,user.mobNum, user.isValidated, user.isActivated, user.gender)}

Use this mapper (just create a kt file with it) to map UserMap class to Map<String, Any>: refer this

val gson = Gson()

//convert a data class to a map
fun <T> T.serializeToMap(): Map<String, Any> {
    return convert()
}

//convert a map to a data class
inline fun <reified T> Map<String, Any>.toDataClass(): T {
    return convert()
}

//convert an object of type I to type O
inline fun <I, reified O> I.convert(): O {
    val json = gson.toJson(this)
    return gson.fromJson(json, object : TypeToken<O>() {}.type)
}

Then Use like this somewhere you wanted to pass on the data:

val userMap = UserMap.ModelMapper.from(User).serializeToMap() 

Now pass userMap to interface:

@JvmSuppressWildcards
internal interface MatchesApi {

    @POST("update/profile")
    @FormUrlEncoded
    fun updatePending(
        @FieldMap userMap: Map<String, Any>
    ):Call<ResponseClass>
}

Dont forget to annotate with @JvmSuppressWildcards otherwise caughtup with retrofit2 throws parameterError(Parameter type must not include a type variable or wildcard) Error. Have Goodtimes.

binrebin
  • 357
  • 4
  • 16
0

Reference -> How to post array in retrofit android

@FormUrlEncoded
@POST("service_name") 
   void functionName(
        @FieldMap Map<String, String> learning_objective_uuids, @FieldMap Map<String, String> user_uuids, @Field("note") String note,
        Callback<CallBackClass> callback
    );

or

@FormUrlEncoded
    @POST("service_name") 
       void functionName(
            @Field("learning_objective_uuids[]") ArrayList<String> learning_objective_uuids, @Field("user_uuids[]") ArrayList<String> user_uuids, @Field("note") String note,
            Callback<CallBackClass> callback
        );
Avinash Ajay Pandey
  • 1,497
  • 12
  • 19