Modge Modge - 3 months ago 19
JSON Question

Android M + Retrofit + JSON : Can't make field constructor accessible

I am currently checking my app for M support. I get this error when I am making API calls with retrofit. This seems to be a json error that I getting. Has anybody else run into it yet? Here is my JSON helper class.

public class JsonHelper {

private static final String JSON_PARSE_ERROR = "Unable to parse JSON: ";
private static Gson sGson;
private static Gson sExposeGson;

static {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
builder.registerTypeAdapter(BlogCategoryDto.class, new BlogCategorySerializer());
...

sGson = builder.create();

builder.excludeFieldsWithoutExposeAnnotation();
sExposeGson = builder.create();
}

public static Gson getGson() {
return sGson;
}

public static Gson getsExposeGsonGson() {
return sExposeGson;
}

public static String toJson(@NonNull Object object) {
return sGson.toJson(object);
}

public static String toJson(@NonNull Object object, @NonNull Type type) {
return sGson.toJson(object, type);
}

public static String toExposeJson(@NonNull Object object) {
return sExposeGson.toJson(object);
}

public static String toExposeJson(@NonNull Object object, @NonNull Type type) {
return sExposeGson.toJson(object, type);
}

public static <T> T fromJson(@NonNull String content, @NonNull Class<T> clazz)
throws IllegalStateException {
try {
return sGson.fromJson(content, clazz);
} catch (JsonSyntaxException e) {
throw new IllegalStateException(JSON_PARSE_ERROR + content, e);
}
}

public static <T> T fromJson(@NonNull String content, @NonNull Type type)
throws IllegalStateException {
try {
return sGson.fromJson(content, type);
} catch (JsonSyntaxException e) {
throw new IllegalStateException(JSON_PARSE_ERROR + content, e);
}
}
}


This is the error that is thrown

D/Retrofitīš•java.lang.SecurityException: Can't make field constructor accessible
at java.lang.reflect.Constructor.setAccessible(Constructor.java:334)
at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:97)
at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:79)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:82)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:52)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:52)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.Gson.fromJson(Gson.java:809)
at com.google.gson.Gson.fromJson(Gson.java:775)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)

Answer

As was pointed out in the comments there is an issue allready opened at Gson project.

https://github.com/google/gson/issues/648

So it seems like a good idea to wait until they update their library. In the mean time I was able to make a quick fix by adding a line of code.

static {
    GsonBuilder builder = new GsonBuilder();

    ...

    builder.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC);**
    builder.excludeFieldsWithoutExposeAnnotation();
    sExposeGson = builder.create();
}