【发布时间】:2014-10-18 00:45:42
【问题描述】:
下面是我需要Android应用以JSON形式传递给REST API的数据的数据结构的描述。特别值得注意的是“响应”字段,它的类型各不相同。有时它是一个布尔值,有时是一个整数,或者它可以是一个字符串或一个整数数组。由于各种原因,服务器 API 不能轻易更改,所以我必须按原样支持它。
如何自定义我的 Retrofit 用途以适应这样的数据类型变化的字段?我之前为 Retrofit 的 rest 适配器编写了类型适配器,它非常适合处理响应,但是请求类型适配器的等价物是什么?
array(
array(
question_id => 1,
route_id => 1,
response => true,
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 2,
route_id => 1,
response => ’this is a string',
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 3,
route_id => 1,
response => 1,
timestamp => ‘2014-01-01 20:01:01'
),
array(
question_id => 4,
route_id => 1,
response => array(
1234,
178,
109
),
timestamp => ‘2014-01-01 20:01:01'
),
)
【问题讨论】: