You must specify Gson type T Since gson does not know which adapter to use, it simply returns a data structure.
You need to provide a generic type, for example:
Type apiResultType = new TypeToken<ApiResult<String>>() { }.getType();
If type T is known only at run time, I use something complicated:
static TypeToken<?> getGenToken(final Class<?> raw, final Class<?> gen) throws Exception { Constructor<ParameterizedTypeImpl> constr = ParameterizedTypeImpl.class.getDeclaredConstructor(Class.class, Type[].class, Type.class); constr.setAccessible(true); ParameterizedTypeImpl paramType = constr.newInstance(raw, new Type[] { gen }, null); return TypeToken.get(paramType); }
Your call will (but replace String.class with a variable):
Type apiResultType = getGenToken(ApiResult.class, String.class);
Pompom
source share