New file |
| | |
| | | package com.hdl.linkpm.sdk.core.response; |
| | | |
| | | |
| | | import android.content.pm.ApplicationInfo; |
| | | import android.provider.Settings; |
| | | import android.widget.Toast; |
| | | |
| | | import com.hdl.hdlhttp.callback.HxException; |
| | | import com.hdl.hdlhttp.response.GsonSubscribe; |
| | | import com.hdl.hdlhttp.utils.ParameterizedTypeImpl; |
| | | import com.hdl.linkpm.sdk.HDLLinkPMSdk; |
| | | import com.hdl.linkpm.sdk.core.code.HDLCloudCode; |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.linkpm.sdk.utils.HDLGsonUtils; |
| | | |
| | | import java.lang.reflect.ParameterizedType; |
| | | import java.lang.reflect.Type; |
| | | |
| | | |
| | | /** |
| | | * Created by Tong on 2021/11/8. |
| | | */ |
| | | public abstract class HDLResponse<K> extends GsonSubscribe<BaseInfo<K>> { |
| | | |
| | | 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(); |
| | | Type parameterizedType = new ParameterizedTypeImpl(BaseInfo.class, types, BaseInfo.class); |
| | | BaseInfo<K> data = HDLGsonUtils.fromJson(s, parameterizedType); |
| | | onSuccess(data); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | this.onError(HxException.handleException(e)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public final void onSuccess(BaseInfo<K> response) { |
| | | if (response.getCode() == HDLCloudCode.SUCCEED) { |
| | | onResponse(response.getData()); |
| | | } else { |
| | | HDLLinkPMSdk.showToast(response.getMessage()); |
| | | onFailure(new HDLException(response.getCode(), response.getMessage(), response.getExtra())); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public final void onError(HxException e) { |
| | | // if(HDLLinkPMSdk.isDebugVersion()){ |
| | | // HDLLinkPMSdk.showToast(e.getRawThrowable().getMessage()); |
| | | // } |
| | | onFailure(new HDLException(e.getCode(), e.getRawThrowable().getMessage(), null)); |
| | | } |
| | | |
| | | } |