inRetrofit请求中字段的日期格式

时间:2014-12-03 18:38:02

标签: android datetime retrofit

我正在使用Retrofit进行REST调用。某些调用需要在form-urlencoded字段中使用日期。使用搜索我找到了方便的方法来使用JSON正文的日期格式化程序模式。只需在.setDateFormat("dd/MM/yyy")上致电GsonBuilder即可。我想知道是否有类似的方法来设置参数Retrofit格式化模式在@Field@Path@Query字段中。 感谢。

2 个答案:

答案 0 :(得分:2)

我不认为它现在以一种不错的方式支持这一点。请作者之一检查此答案(具体涉及日期问题):https://github.com/square/retrofit/issues/291

我已经在这里复制了GitHub问题答案中的代码。

public class PathDate {
  private static final ThreadLocal<DateFormat> DF = new ThreadLocal<DateFormat>() {
    @Override public DateFormat initialValue() {
      return new SimpleDateFormat("THE FORMAT YOU WANT HERE!!!!");
    }
  }

  private final Date date;

  public PathDate(Date date) {
    this.date = date;
  }

  @Override public String toString() {
    return DF.get().format(date);
  }
}

现在您的Retrofit代码如下所示

@GET("/programs/date/{date}")
void findByDate(@Path("date") PathDate date, Callback<List<Program>> cb);

答案 1 :(得分:0)

对于Retrofit 2,可以设置自定义Converter.Factory

System.Type objBLType = System.Type.GetTypeFromProgID("Catia.Application");
object objBL = System.Activator.CreateInstance(objBLType);

}

您可以添加自己的类型和转换器。

相关问题