Android M + Retrofit + JSON:无法使字段构造函数可访问 [英] Android M + Retrofit + JSON : Can't make field constructor accessible
本文介绍了Android M + Retrofit + JSON:无法使字段构造函数可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在检查我的应用程序是否支持M.当我进行翻新的API调用时出现此错误.这似乎是我得到的json错误.还有其他人遇到吗?这是我的JSON助手类.
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);
}
}
}
这是引发的错误
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)
推荐答案
正如评论中指出的那样,Gson项目已经开放了一个问题.
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();
}
这篇关于Android M + Retrofit + JSON:无法使字段构造函数可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文