Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Retrofit requestbody using @FieldMap

Posted on November 14, 2022

Solution 1 :

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
        );

Solution 2 :

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.

Problem :

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.

READ  [ANSWERED] java - httpclient.client.HttpResponseException: Too Many Requests
Powered by Inline Related Posts

Comments

Comment posted by binrebin

I think I need to map my app-specific-fields to api-specific-fiels first. thats how?

Comment posted by Avinash Ajay Pandey

create map and put key then value, do the same thing for all the field name you have, and that map into the function.

Comment posted by Avinash Ajay Pandey

great you achieved this enjoy

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress