gpt4 book ai didi

java - java中如何获取泛型类的类型?

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:37 26 4
gpt4 key购买 nike

public class Network_Generic {

public static <T> void get_generic(String url, final VolleyCallback volleyCallback,T t)
{
Network_GetString.get_String(url,new VolleyCallback() {
@Override
public void onSuccess(Object o) {

String json=(String)o;
Gson gson=new Gson();
java.lang.reflect.Type type = new TypeToken<T>() {}.getType();

T jsonBean = gson.fromJson(json, type);
volleyCallback.onSuccess(jsonBean);

}

@Override
public void onFailure(String error) {

volleyCallback.onFailure(error);

}
});
}
public class Network_Location {
public static void get_locations(final Integer id,final VolleyCallback volleyCallback)
{
String url="/location/view/"+id.toString()+".json";
Network_Generic.get_generic(url,new VolleyCallback() {
@Override
public void onSuccess(Object o) {
Json_Location_Bean json_location_bean=(Json_Location_Bean)o;
volleyCallback.onSuccess(json_location_bean);
}

@Override
public void onFailure(String error) {
volleyCallback.onFailure(error);

}
},new Json_Location_Bean()

);

}

这里我想在方法get_String中获取“T”的类型,但失败了。编译器告诉我“java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为 Json_Location_Bean。有人可以帮我得到T的类型吗?谢谢!

最佳答案

怎么样

t.getClass().getName()

关于java - java中如何获取泛型类的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com