samedi 24 janvier 2015

How to get the type of a generic class in java?


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

);

}


Here I would like to get the type of "T" in method get_String,but failed. The compiler told me that "java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Json_Location_Bean. Could someone help me to get the type of T? Thanks!






Aucun commentaire:

Enregistrer un commentaire