【问题标题】:Android - Retrofit being received as null when the model contains a byte[]Android - 当模型包含一个字节 [] 时,Retrofit 被接收为 null
【发布时间】:2014-03-02 18:17:51
【问题描述】:

我正在使用 Retrofit 将一些数据发布回我的 WebAPI REST 服务。但是,如果我的模型包含byte[],则 WebAPI 接收到的值为 null。如果我删除属性Signature,则模型将按预期与其余值一起接收。

此外,将Signature 属性保留为空也可以。只有当签名有内容时,WebAPI 才会收到空值。

字节数组包含设备上捕获的PNG签名图像。

这是我的模型,包含 byte[] 属性:

public class RefundDto {
    public String Id;
    public String Amount;
    public int AssetId;
    public String Comments;
    public String DateCreated;
    public String DateModified;
    public int FaultId;
    public int RefundActionId;
    public int SiteId;
    public int UserId;

    public byte[] Signature; // The culprit
}

我使用以下代码发送我的模型:

RefundDto dto = getDto();
service.postRefund(dto, new Callback<RefundDto>() {
    @Override
    public void success(RefundDto dto, Response response) {
        DatabaseHandler db = new DatabaseHandler(context);
        db.deleteRefund(dto.Id);
        db.close();
    }

    @Override
    public void failure(RetrofitError retrofitError) {
    }
});

Retrofit 的 DataService 方法如下所示:

@POST("/refunds")
void postRefund(@Body RefundDto model, Callback<RefundDto> callback);

我将其发送到以下休息方法:

public class RefundsController : ApiController
{
    public RefundDto Post([FromBody]RefundDto value)
    {
        // value is null!
    }
}

【问题讨论】:

    标签: java android asp.net-web-api retrofit


    【解决方案1】:

    我设法对此进行了排序。我将byte[] 更改为String 并将签名作为Base64 编码字符串发送:

    dto.Signature = new String(Base64.encode(getSignature(), 0));
    

    我使用byte[] 离开了我的服务器端模型,WebAPI 负责转换。

    【讨论】:

    • 这是迄今为止我发现的唯一方法。
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2017-01-20
    • 2020-02-09
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多