获取类:
public static <T>T getT(Object o,int i){ ParameterizedType parameterizedType = (ParameterizedType) o.getClass().getGenericSuperclass(); Type type = parameterizedType.getActualTypeArguments()[i]; Class<T> t = (Class<T>) type; try { T t1 = t.newInstance(); return t1; } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } return null; }
获取接口:
try { String data = responseBody.string(); Type[] types = netCallBack.getClass().getGenericInterfaces(); Type[] ty = ((ParameterizedType) types[0]).getActualTypeArguments(); Type t = ty[0]; Gson gson = new Gson(); T gson_FT = null; gson_FT = gson.fromJson(data, t); netCallBack.onSuccess(gson_FT); } catch (IOException e) { e.printStackTrace(); }