使用Gson接受特定类的方法,但实现是未知的

时间:2014-06-18 21:34:04

标签: java gson

假设有一个方法接口:

public void receiveResult(ApiResponse apiResponse);

ApiResponse是其他“响应”类的子类。

现在还有一个Runnable,它在代码中的某个地方执行:

interfaceCallback.receiveResult(new Gson().fromJson(someHttpResponse, ???);

Runnable的构造函数需要能够获取任何类对象:

new MyRunnable(SomeResponse.class); (SomeResponse is a subclass of ApiResponse)
new MyRunnable(AnotherResponse.class); (AnotherResponse is a subclass of ApiResponse)

这可能吗?我一直在尝试编写构造函数,如下所示:

public MyRunnable(Class<? extends ApiResponse> passedClass) {
// set fields
}

但是当我尝试实例化MyRunnable时,我无法执行以下操作:

new MyRunnable(SomeResponse.class);
new MyRunnable(AnotherResponse.class);

我希望这是有道理的。

编辑:

public interface Callback {
    public void receiveResult(ApiResponse apiResponseClass);
}

public class RunnableRequest {
     public <T extends ApiResponse> RunnableRequest(Class<T> passedApiResponseClass, Callback callback) {
          this.passedApiResponseClass = passedApiResponseClass;
          this.callback = callback;
     }

     @Override
     public void run() {
          callback.receiveResult(new Gson().fromJson(httpResponseString,    passedApiResponseClass));
     }
}

错误在这里(另一个类):

new RunnableApiGetRequest(MyResponse.class, callback);

错误: 无法应用 预期 类 实际 MyResponse.class

编辑#3:

// I am not sure how to define the object that is passed here.
static RunnableRequest createRequestRunnable(???, Callback callback) {
    return new RunnableRequest(???, callback);
}

public class RunnableRequest implements Runnable {
    // Here is where I am not sure exactly how to pass, for example, SystemStatusResponse.class OR ApiResponse.class
    // OR SomeOtherResponse.class OR Whatever.class
    public <T extends ApiResponse> RunnableRequest(???, Callback callback) {
        this.callback = callback;
        this.apiResponse = apiResponse;
    }

    @Override
    public void run() {
        String exampleResponse = "{\"result\":\"Success\",\"message\":\"Hello World\",\"error\":\"0\"}";
        // not sure what to put here. normally I can just pass SystemStatusResponse.class or ApiResponse.class
        // or any other subclass of ApiResponse 
        callback.apiRequestProcessFinished(new Gson().fromJson(getResponseString(exampleResponse), ???));

    }
}

public interface Callback {
    // I am not 100% sure on how to define this interface because the "apiResponse" should of the type
    // that was originally passed to the method "createRequestRunnable"
    public void apiRequestProcessFinished(ApiResponse apiResponse);
}

// sample Gson response class
public class ApiResponse {
    private String message;
    private String result;
    private String error;

    public final String getMessage() { return message; }
    public final String getError() { return error; }
    public final String getResult() { return result; }
}

// sample Gson response class which subclasses ApiResponse
public class SystemStatusResponse extends ApiResponse {
    private String status;
    private String uptime;
    private String lastdowntime;
    public String getStatus() { return status; }
    public String getUptime() { return uptime; }
    public String getLastDownTime() { return lastdowntime; }
}

1 个答案:

答案 0 :(得分:0)

它可能对你有所帮助。使类或接口 Generic 而不是如下所示的方法:

public class RunnableRequest<T extends ApiResponse> implements Runnable {
    private Class<T> apiResponse;
    private Callback<T> callback;

    public RunnableRequest(Class<T> apiResponse, Callback<T> callback) {
        this.callback = callback;
        this.apiResponse = apiResponse;            
    }

    @Override
    public void run() {
        String exampleResponse = "{\"result\":\"Success\",\"message\":\"Hello World\",\"error\":\"0\"}";            
        callback.apiRequestProcessFinished(new Gson().fromJson(exampleResponse, apiResponse));

    }
}

public interface Callback<T extends ApiResponse> {       
    public void apiRequestProcessFinished(T apiResponse);
}

static <T extends ApiResponse> RunnableRequest<T> createRequestRunnable(Class<T> apiResponse,
        Callback<T> callback) {
    return new RunnableRequest<T>(apiResponse, callback);
}