我是使用Java泛型並面臨其語法問題的新手。我試着解決這個問題很長時間,在互聯網和計算器上閱讀了很多文章,但找不到解決方案。但我不相信有解決這個problem-Java泛型的語法
所以我有泛型類如下 -
public class GsonRequest<T> extends Request<T> {}
我在此方法中創建該類的對象:
private <T> void makeGsonWebserviceRequest(int requestType, final String requestEndPoint,
Object requestData, Type responseClass, Map<String, String> headers,
final Handler successHandler, final Handler failureHandler) {
final GsonRequest request;
try {
request = new GsonRequest<>(requestType, requestEndPoint, requestData, responseClass, headers,
new Response.Listener<JaiSamadhiServiceBaseListResponse>() {
@Override
public void onResponse(JaiSamadhiServiceBaseListResponse response) {
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
});
// Add the request to the RequestQueue.
addRequestToQueue(request);
}
我調用這樣這個方法:
TypeToken<JaiSamadhiServiceBaseListResponse<JsContact>> responseTypeToken =
new TypeToken<JaiSamadhiServiceBaseListResponse<JsContact>>() {};
makeGsonWebserviceRequest(Request.Method.GET,
context.getString(R.string.BASEURL) + context.getString(R.string.GETCONTACTLIST),
null, responseTypeToken.getType(), createHeader(), successHandler, failureHandler);
所以我的問題是:
如何通過<JaiSamadhiServiceBaseListResponse<JsContact>>
,作爲通用類型,在new GsonRequest<>
它位於makeGsonWebserviceRequest
方法?
作爲參考,下面是該模型類:
public class JaiSamadhiServiceBaseListResponse<T> {
@SerializedName("Version")
protected String version;
@SerializedName("StatusCode")
protected int statusCode;
@SerializedName("ErrorMessage")
protected String errorMessage;
@SerializedName("Result")
protected ArrayList<T> result;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public ArrayList<T> getResult() {
return result;
}
public void setResult(ArrayList<T> result) {
this.result = result;
}
@Override
public String toString() {
return "JaiSamadhiServiceBaseObjectResponse{" +
"version='" + version + '\'' +
", statusCode=" + statusCode +
", errorMessage='" + errorMessage + '\'' +
", result=" + result +
'}';
}
}
'最終的GsonRequest請求;'應該已經給你一個使用原始類型的警告。爲什麼不是這個'GsonRequest'? –
dhke
「如何通過」作爲通用類型「究竟是什麼」>「? 你期望你的泛型類型是什麼? – Bifz
您定義的方法:'最終的GsonRequest請求;' - 爲什麼不:'最終的GsonRequest請求;' –