Android 在 Kotlin 上改造我如何为响应添加时间戳?
Android Retrofit on Kotlin how i can add timestamp to response?
我有一个简单的 Json class 可以完美地处理改装。
但是里面没有时间戳。
我添加了不在 json 中的自定义字段,但它始终为 null
//Custom field for timestamp
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
//My class for JSON respons
@Entity(tableName = "nal")
data class CurrencyItem(
@PrimaryKey(autoGenerate = true)
val id:Long,
@SerializedName("base_ccy")
var baseCcy: String,
@SerializedName("buy")
val buy: String,
@SerializedName("ccy")
val ccy: String,
@SerializedName("sale")
val sale: String,
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
)
最简单的解决方案是在模型中为时间戳添加自定义字段。
val timeStamp: String
-当改造响应到来时,用时间戳重写这个空字段,我使用方法 SimpleDateFormat
// retrofit response
var res = resp.body()
// new list which i create and rewrite with timestamp and then return
var l = ArrayList<CurrencyItem>()
//read response list with json converted data
for (r in res.orEmpty()){
l.add(CurrencyItem(1 ,
r.baseCcy,
r.buy,
r.ccy,
r.sale,
SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())))
}
//return this new list with timestamp. I use repository pattern with livedata
response.value = l
我有一个简单的 Json class 可以完美地处理改装。 但是里面没有时间戳。 我添加了不在 json 中的自定义字段,但它始终为 null
//Custom field for timestamp
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
//My class for JSON respons
@Entity(tableName = "nal")
data class CurrencyItem(
@PrimaryKey(autoGenerate = true)
val id:Long,
@SerializedName("base_ccy")
var baseCcy: String,
@SerializedName("buy")
val buy: String,
@SerializedName("ccy")
val ccy: String,
@SerializedName("sale")
val sale: String,
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
)
最简单的解决方案是在模型中为时间戳添加自定义字段。
val timeStamp: String
-当改造响应到来时,用时间戳重写这个空字段,我使用方法 SimpleDateFormat
// retrofit response
var res = resp.body()
// new list which i create and rewrite with timestamp and then return
var l = ArrayList<CurrencyItem>()
//read response list with json converted data
for (r in res.orEmpty()){
l.add(CurrencyItem(1 ,
r.baseCcy,
r.buy,
r.ccy,
r.sale,
SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())))
}
//return this new list with timestamp. I use repository pattern with livedata
response.value = l