我正在尝试将 gson 字符串解析为我的 java 类,但由于照片的原因,我不断收到异常。
我正在使用 MySQL,带有用于图像的 Blob 列。
我在Android和服务器中的java类是这样的
public class Cliente {
private Integer id;
private byte[] foto;
private String nome;
private Date data_nascimento;
private String endereco;
private String telefone;
private boolean ativo;
private Date data_cadastro;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public byte[] getFoto() {
return foto;
}
public void setFoto(byte[] foto) {
this.foto = foto;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Date getData_nascimento() {
return data_nascimento;
}
public void setData_nascimento(Date data_nascimento) {
this.data_nascimento = data_nascimento;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public boolean isAtivo() {
return ativo;
}
public void setAtivo(boolean ativo) {
this.ativo = ativo;
}
public Date getData_cadastro() {
return data_cadastro;
}
public void setData_cadastro(Date data_cadastro) {
this.data_cadastro = data_cadastro;
}
}
JSON 字符串列表(由于字符有限,foto 字段上有 NULL)
[{"id":1,"foto":null,"nome":"Guilherme","data_nascimento":"1993-12-23","endereco":"Rua Jornalista Angelo Zanuzzi, 560","telefone":"37055056","ativo":true,"data_cadastro":"2015-08-26"},{"id":2,"foto":null,"nome":"Lucas","data_nascimento":"2015-09-02","endereco":"Rua Jornalista Angelo Zanuzzi, 560","telefone":"37055056","ativo":true,"data_cadastro":"2015-08-26"}]
解析
ResponseEntity<?> responseEntity = Rest.makeRequest("services/clientes", HttpMethod.GET, null);
clienteList = gson.fromJson(responseEntity.getBody().toString(), new TypeToken<List<Cliente>>() {
在 foto 字段上使用 NULL 时,gson 效果很好,但是当图像来 self 的数据库时,它会抛出以下异常
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 18 path $[0].foto
我不知道如何将字符串解析为字节数组!
编辑
我的 JSON 是正确的,当我复制/粘贴时我忘记添加 []。
这是 Json 的开头,其中包含来自 mysql/hibernate 的图像(blob)
由于代码太长,我无法发布完整的代码
[{"id":1,"foto":"/9j/4AAQSkZJRgABAgAAAQABAAD/7QA2UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAABkcAmcAFEphUnk5alpFY3RFUWo5Zy1adUxSAP/iC/hJQ0NfUFJPRklMRQABAQAAC+gAAAAAAgAAA
然后,当 GSON 尝试转换时,我收到错误。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 18 path $[0].foto
问题是,HIBERNATE 将照片作为 STRING 而不是 BYTE[] 数组发送
然后,当我尝试解析时,GSON 抛出异常。 如果我将 Android 模型照片更改为字符串(私有(private)字符串照片),gson 会正确解析,但我无法弄清楚如何将此“字节字符串”放入 ImageView 中。
我想接收字节[]并将其设置为 ImageView
最佳答案
解决方案
实际上,Hibernate 将照片数据作为字符串发送回来,所以我必须将 android 中的模型更改为
private String foto;
而不是
private byte[] foto;
并且,在将其转换为我的 ImageView 之前,我必须使用 来解码字符串
byte[] bytes = Base64.decode(itemCardCliente.getFoto(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
imageView.setImageBitmap(bitmap);
关于java - GSON 预期为 BEGIN_ARRAY,但实际为 STRING,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389973/