Retrofit: How to send a POST request with constant fields?

后端 未结 4 1309
旧时难觅i
旧时难觅i 2021-02-19 09:29

I want to sent a simple POST request with one actual parameter:

@POST(\"/token\")
@FormUrlEncoded
void extendSession(@Field(\"refresh_token\")final String refres         


        
4条回答
  •  情话喂你
    2021-02-19 10:26

    It is matter of your approach. If you have the constants you can build a default Map of values required for your call. @FieldMap will be suitable for building a Map with all your required fields

    private void extendSession(String token){
       Map params = buildDefaultParams();
       params.put("refreshToken", token);
       getRestAdapter().create(MyApi.class).extendsSession(params);
    }
    private Map buildDefaultParams(){
       Map defaults = new HashMap();
       defaults.put("client_id", CLIENT_ID);
       defaults.put("client_secret", CLIENT_SECRET);
       defaults.put("grant_type", GRANT_TYPE);
       return defaults;
    }
      /**then you change your interface to this **/ 
        @POST("/token")
        @FormUrlEncoded
        void extendSession(@FieldMap() Map refreshToken);
    

提交回复
热议问题