我想將我的previous post中的代碼抽象爲一個util類。這裏的UTIL類方法:Java方法參數和返回類對象
private static Gson gson = new GsonBuilder().create();
public static Class getResponseObject(String resourceResponse, String jsonObject, Class responseClass) {
JSONObject jsonResponse = new JSONObject(resourceResponse);
String jsonResponseToString = jsonResponse.getJSONObject(jsonObject).toString();
return gson.fromJson(jsonResponseToString, responseClass.getClass());
}
這是從另一個類調用:
UserIdentifier userIdentifier = ServiceClientUtil.getResponseObject(resourceResponse,
"userIdentifier",
UserIdentifier.class);
但我發現了以下錯誤:
Error:(68, 76) java: incompatible types: java.lang.Class cannot be converted to app.identity.UserIdentifier
如何傳遞一個類對象並返回相同的類對象?
該錯誤信息已足夠清楚。你對此不瞭解什麼? – Raedwald