我有一个java SDK
,它使用OkHttp
客户端(4.0.0)从服务器获取令牌IAM
并将令牌返回给应用程序。关系可能是这样的:Applicaiton Sync call SDK
,SDK
Async call IAM
。参考这个答案Java - Retrieving Result from OkHttp
Asynchronous GET,the代码如下:
异步类:
class BaseAsyncResult<T> {
private final CompletableFuture<T> future = new CompletableFuture<>();
T getResult() {
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return null;
}
void onFailure(IOException e) {
future.completeExceptionally(e);
}
void onResponse(Response response) throws IOException {
String bodyString = Objects.requireNonNull(response.body()).string();
future.complete(IasClientJsonUtil.json2Pojo(bodyString, new TypeReference<T>() {}));
}
}
Okhttp 调用如下:
public void invoke(Request request, BaseAsyncResult result) {
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
result.onFailure(e);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
result.onResponse(response);
}
});
}
该应用程序使用 sdk 代码,如 iasClient 是 okhttp 客户端的包装器:
BaseAsyncResult<AuthenticationResponse> iasAsyncResult = new BaseAsyncResult();
iasClient.invoke(request, iasAsyncResult);
AuthenticationResponse result = iasAsyncResult.getResult();
错误消息:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to x.x.x.AuthenticationResponse
我错过了什么?
慕仙森
收到一只叮咚
相关分类