Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 910
Eoin Avatar asked Sep 07 '15 04:09

Eoin


3 Answers

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();
}
like image 170
Eoin Avatar answered Oct 17 '22 17:10

Eoin


This Worked for me. Hope this could help someone.

GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC);
Gson gson = builder.create();
like image 34
Muneef M Avatar answered Oct 17 '22 18:10

Muneef M


Best way!

  GsonBuilder builder = new GsonBuilder().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC);
  Gson gson = builder.create();
  Retrofit retrofit = new Retrofit.Builder().baseUrl(URL)
      .addConverterFactory(GsonConverterFactory.create(gson))
      .build();
like image 36
Pierry Avatar answered Oct 17 '22 18:10

Pierry