Android M + Retrofit + JSON:无法使字段构造函数可访问

我目前正在检查我的应用程序的M支持。 当我使用改进的API调用时出现此错误。 这似乎是我得到的JSON错误。 还有其他人遇到它吗? 这是我的JSON助手类。

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); } } } 

这是抛出的错误

 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) 

  • Android中的严格性及其使用
  • @IntDef Android支持与jackson反序列化的公告
  • Gson - 相同的字段名称,不同的types
  • 当试图在json中parsingdate/时间时com.google.gson.JsonSyntaxException
  • Java.lang.IllegalStateException:已连接
  • 开发具有相同API的网站
  • JSONObject.toString:如何不转义斜杠
  • 使用Volley发送post请求并以PHP格式接收
  • 正如在评论中指出的那样,在Gson项目上已经有一个问题已经被公开。

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

    所以,等到他们更新他们的图书馆似乎是一个好主意。 同时,我可以通过添加一行代码来快速修复。

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

    这为我工作。 希望这可以帮助别人。

     GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC); Gson gson = builder.create(); 

    Android M中使用Retrofit2存在一个问题,如上所述

    其中一个解决scheme是像这样configurationGsonConverterFactory:

     Retrofit retrofit = new Retrofit.Builder() .baseUrl(URLWebService) .addConverterFactory(GsonConverterFactory.create( new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create() )) .client(okHttpClient) .build(); 

    最好的办法!

      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();