package com.hdl.sdk.connect.cloud; import com.hdl.hdlhttp.callback.HxException; import com.hdl.hdlhttp.response.GsonSubscribe; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; /** * Created by Tong on 2021/11/8. */ public abstract class HDLResponse extends GsonSubscribe> { public abstract void onResponse(K response); public abstract void onFailure(HDLException e); @Override public void onNext(String s) { try { Type type = getClass().getGenericSuperclass(); if (type instanceof ParameterizedType) { Type[] types = ((ParameterizedType) type).getActualTypeArguments(); ParameterizedTypeImpl parameterizedType = new ParameterizedTypeImpl(BaseInfo.class, types, BaseInfo.class); BaseInfo data = GsonUtils.fromJson(s,parameterizedType); onSuccess(data); } } catch (Exception e) { e.printStackTrace(); this.onError(HxException.handleException(e)); } } @Override public final void onSuccess(BaseInfo response) { if (response.getCode() == CloudLinkCode.SUCCEED) { onResponse(response.getData()); } else { onFailure(new HDLException(response.getCode(), response.getMessage())); } } @Override public final void onError(HxException e) { onFailure(new HDLException(e)); } }