改造:如何发送具有恒定领域POST请求? [英] Retrofit: How to send a POST request with constant fields?

查看:138
本文介绍了改造:如何发送具有恒定领域POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发一个简单的POST请求与一个实际的参数:

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

@POST("/token")
@FormUrlEncoded
void extendSession(@Field("refresh_token")final String refreshToken);

但这个请求也应当发送服务器请求某个常数的值,如的client_id client_secret grant_type 这是不变的,不应该是应用程序API的一部分。

But this request should also send some constant values requested by the server such as client_id, client_secret and grant_type which are constant and should not be part of the application API.

什么是做到这一点的最好方法是什么?

What is the best way to do this?

推荐答案

这是你的方法的问题。如果你有常量,你可以建立你的电话所需的值的默认地图。 @FieldMap 将适合建设有你的所有必填字段一个地图

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

这篇关于改造:如何发送具有恒定领域POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆