Gson 解析 byte[] 出错

增加byte[]的adapter类

Gson mGson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, new ByteArrayToBase64TypeAdapter()).create();

//增加byte[]的adapter类
 private static class ByteArrayToBase64TypeAdapter implements JsonSerializer, JsonDeserializer {
        public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return Base64.decode(json.getAsString(), Base64.NO_WRAP);
        }

        public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(Base64.encodeToString(src, Base64.NO_WRAP));
        }
    }

你可能感兴趣的:(Gson 解析 byte[] 出错)