Question

I'm trying to get a blob from my mysql but using json. Here is my code:

public class ClubVO {
    private long _id;
    private String nom = null;
    private byte[] imageClub = null;
    public long getId() {
        return _id;
    }
    public void setId( long aId ) {
        this._id = aId;
    }
    public String getNom() {
        return nom;
    }
    public void setNom( String aNom ) {
        this.nom = aNom;
    }
    public byte[] getImage() {
        return imageClub;
    }
    public void setImage( byte[] aImage ) {
        this.imageClub = aImage;
    }
}

Here is the function I use to retrieve my json object:

public List<ClubVO> consultPlacesByName() throws Exception {
    String[] resposta = new WebServiceClient().get( URL_WS );

    if ( resposta[0].equals("200") ) {
        Gson gson = new Gson();
        ArrayList<ClubVO> lListaPlaces = new ArrayList<ClubVO>();
        JsonParser parser = new JsonParser();
        JsonArray lJasonArray = parser.parse(resposta[1]).getAsJsonArray();
        for (int i = 0; i < lJasonArray.size(); i++)
            lListaPlaces.add( gson.fromJson( lJasonArray.get(i), ClubVO.class ) );
        return lListaPlaces;
    } else {
        throw new Exception( resposta[1] );
    }
}

Here is what I am getting with this code:

{"_id":15,"nom":"Musée d'art Contemporain de Montréal","imageClub":"[B@25450aa2"}

Just one more thing, my column is blob type.

Was it helpful?

Solution

Your problem is that instead of the real image bytes you have the hashcode of the byte array in the json data: "[B@25450aa2" To solve the problem when you serialize the object you need to use

new String(Base64.encodeBase64(byteArray))

and when you parse the json data you need to do the opposite:

Base64.decodeBase64(jsonString.getBytes())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top