亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

返回 OkHttp 異步結果有問題

返回 OkHttp 異步結果有問題

三國紛爭 2023-04-13 10:25:31
我有一個java SDK,它使用OkHttp客戶端(4.0.0)從服務器獲取令牌IAM并將令牌返回給應用程序。關系可能是這樣的:Applicaiton Sync call SDK,SDKAsync call IAM。參考這個答案Java - Retrieving Result from OkHttpAsynchronous 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我錯過了什么?
查看完整描述

2 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

您需要確保 jackson 知道將值反序列化到哪個類。在這種情況下,您要求 Jackson 將響應反序列化為 TypeReference ,默認情況下它將解析為 Map ,除非您指定類(在本例中為 AuthenticationResponse )。因此, Future 解析為 linkedHashMap 并導致類轉換。嘗試替換下面的行。

future.complete(IasClientJsonUtil.json2Pojo(bodyString, new TypeReference<T>() {}));

future.complete(IasClientJsonUtil.json2Pojo(bodyString, new TypeReference<AuthenticationResponse>() {}));



查看完整回答
反對 回復 2023-04-13
?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

一種方法是將私有類類型變量添加到 BaseAsyncResult,然后在 json2Pojo 函數中使用該類,然后 BaseAsyncResult 可能如下所示:


public class BaseAsyncResult<T> {

    private final CompletableFuture<T> future = new CompletableFuture<>();

    private Class<T> classType;


    public BaseAsyncResult(Class<T> classType) {

        this.classType = classType;

    }


    public 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 {

        future.complete(JacksonUtil.json2Pojo(response.body().string(), classType));

    }

}


查看完整回答
反對 回復 2023-04-13
  • 2 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號