私の GSON の問題はすべて、私の戻り値の型はパラメーター化されたオブジェクトではありませんが、そうであるべきだったという事実に関するものであることがわかりました。ここで、パラメーターの型を指定して Gson.fromJson メソッドを使用し、戻り値の型を指定して、GSON が処理してくれるようにする必要があります。
次のような RestResponse という汎用クラスを作成しました。
public class RestResponse<T> {
private String errorMessage;
private int errorReason;
private T result;
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "RestResponse [errorMessage=" + errorMessage + ", result=" + result + "]";
}
/**
* Does this response contain an error?
* @return true if in error
*/
public boolean isInError(){
return getErrorMessage()!=null;
}
/**
* @return the errorMessage
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* @param errorMessage the errorMessage to set
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
/**
* The error reason code
* @return the errorReason
*/
public int getErrorReason() {
return errorReason;
}
/**
* The error reason code
* @param errorReason the errorReason to set
*/
public void setErrorReason(int errorReason) {
this.errorReason = errorReason;
}
/**
* The result of the method call
* @return the result or null if nothing was returned
*/
public final T getResult() {
return result;
}
/**
* The result of the method call
* @param result the result to set or null if nothing was returned
*/
public final void setResult(T result) {
this.result = result;
}
}
今、反対側で結果の型を作成したいと思います。これらをデコードし、例外をスローするか、結果を返すために使用する汎用メソッドがあります。
だから私の方法は次のようなものです:
public Object submitUrl(String url, Class<?> clazz) throws AjApiException {
clazz は、RestResponse で指定されるタイプです。
次に、GSON に渡す前に RestResponse を作成します。
Type typeOfT = new TypeToken<RestResponse<clazz>>(){}.getType(); //1-->What goes here?
RestResponse<clazz> restResponse; //2-->and here?
そして、それはエラーです。clazzの代わりにこれらの場所に何が入っているのか誰か教えてもらえますか?