Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • 奇葩代码报错了

    post by onelong / 2013-12-8 22:29 Sunday [java]

    import com.google.gson.Gson;

    import com.google.gson.annotations.Expose;

    import com.google.gson.reflect.TypeToken;

     

    public class MyTask<T> {

    public final String json = "{\"body\":{\"error\":\"eeee\",\"isCustom\":\"False\"}," +

    "\"header\":{\"responseCode\":\"0000\",\"responseMessage\":\"成功\"}}";

     

    public static interface MyTaskCallback<T> {

    public void onStart();

    public void onError(String error);

    public void onSuccessed(T body);

    }

     

    public static class Header {

    @Expose

    public String responseCode;

    @Expose

    public String responseMessage;

    }

     

    public static class Body {

    @Expose

    public String error;

    @Expose

    public String isCustom;

    }

     

    public static class Response<T> {

    @Expose

    public Header header;

    @Expose

    public T body;

    }

     

    private MyTaskCallback<T> callback;

     

    public MyTask(MyTaskCallback<T> callback) {

    this.callback = callback;

    }

     

    public void start() {

    run();

    }

     

    protected void run() {

    if (callback != null) {

    callback.onStart();

    }

     

    T body = null;

    Gson gson = new Gson();

    Response<T> r = gson.fromJson(json, new TypeToken<Response<T>>() {

    }.getType());

    body = r.body;

     

    if (callback != null) {

    callback.onSuccessed(body);

    }

    }

     

    public static void main(String[] args) {

     

    MyTaskCallback<Body> c = new MyTaskCallback<Body>() {

     

    @Override

    public void onSuccessed(Body body) {

     

    }

     

    @Override

    public void onStart() {

     

    }

     

    @Override

    public void onError(String error) {

     

    }

    };

     

    new MyTask<Body>(c).start();

    }

     

    }

     

    异常

    Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.ways2u.gson.MyTask1$Response

    at com.ways2u.gson.MyTask1$2.onSuccessed(MyTask1.java:1)

    at com.ways2u.gson.MyTask1.run(MyTask1.java:60)

    at com.ways2u.gson.MyTask1.start(MyTask1.java:45)

    at com.ways2u.gson.MyTask1.main(MyTask1.java:84)

    最后的解决方案是:

     

    import java.lang.reflect.Type;

     

    import com.google.gson.Gson;

    import com.google.gson.annotations.Expose;

    import com.google.gson.reflect.TypeToken;

     

    public class MyTask<T> {

    public final String json = "{\"body\":{\"error\":\"eeee\",\"isCustom\":\"False\"},"

    + "\"header\":{\"responseCode\":\"0000\",\"responseMessage\":\"�ɹ�\"}}";

     

    public static interface MyTaskCallback<T> {

    public void onStart();

     

    public void onError(String error);

     

    public void onSuccessed(T body);

    }

     

    public static class Header {

    @Expose

    public String responseCode;

    @Expose

    public String responseMessage;

    }

     

    public static class Body {

    @Override

    public String toString() {

    return "Body [error=" + error + ", isCustom=" + isCustom + "]";

    }

     

    @Expose

    public String error;

    @Expose

    public String isCustom;

    }

     

    public static class Response<T> {

    @Expose

    public Header header;

    @Expose

    public T body;

    }

     

    private MyTaskCallback<T> callback;

    private final Type type;

     

    public MyTask(MyTaskCallback<T> callback, Type type) {

    this.callback = callback;

    this.type = type;

    }

     

    public void start() {

    run();

    }

     

    protected void run() {

    if (callback != null) {

    callback.onStart();

    }

     

    T body = null;

    Gson gson = new Gson();

     

    Type t = new TypeToken<Response<T>>() {

    }.getRawType();

    System.out.println(type);

    System.out.println(t);

    System.out.println(new TypeToken<Response<T>>() {

    }.getType());

     

    Response<T> r = gson.fromJson(json, type);

    body = r.body;

     

    if (callback != null) {

    callback.onSuccessed(body);

    }

    }

     

    public static void main(String[] args) {

     

    MyTaskCallback<Body> c = new MyTaskCallback<Body>() {

     

    @Override

    public void onSuccessed(Body body) {

    System.out.println(body);

    }

     

    @Override

    public void onStart() {

     

    }

     

    @Override

    public void onError(String error) {

     

    }

    };

     

    new MyTask<Body>(c, new TypeToken<Response<Body>>() {

    }.getType()).start();

    }

     

    }

    出现这样的原因,归结是java 泛型原理,编译时检测的

    标签: java
    引用地址:
     

    我要评论