New file |
| | |
| | | package com.hdl.sdk.link.core.utils; |
| | | |
| | | import android.text.TextUtils; |
| | | |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.sdk.link.common.utils.LogUtils; |
| | | import com.hdl.sdk.link.common.utils.gson.GsonConvert; |
| | | |
| | | import com.hdl.sdk.link.core.bean.LinkResponse; |
| | | import com.hdl.sdk.link.core.bean.response.BaseLocalResponse; |
| | | import com.hdl.sdk.link.core.bean.response.BaseLocalWithCodeResponse; |
| | | |
| | | import java.lang.reflect.Type; |
| | | |
| | | /** |
| | | * Created by jlchen on 1/6/22. |
| | | */ |
| | | public class LinkResponseUtils<T> { |
| | | |
| | | public Type getType(){ |
| | | return new TypeToken<BaseLocalResponse<T>>() {}.getType(); |
| | | } |
| | | |
| | | /** |
| | | * 转换提取LinkResponse里面的objects |
| | | * |
| | | * @param msg |
| | | * @return |
| | | */ |
| | | public static <T> T convertLinkResponse(Object msg, Type type) { |
| | | T bean = null; |
| | | if (msg != null && msg instanceof LinkResponse) { |
| | | LinkResponse linkResponse = (LinkResponse) msg; |
| | | String data = linkResponse.getData(); |
| | | if (!TextUtils.isEmpty(data)) { |
| | | try { |
| | | final BaseLocalResponse<T> response = GsonConvert.getGson().fromJson(data, type); |
| | | if (response != null) { |
| | | bean = response.getObjects(); |
| | | } |
| | | } catch (Exception e) { |
| | | LogUtils.e("convertLinkResponse catch:" + e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | return bean; |
| | | } |
| | | |
| | | /** |
| | | * 转换提取LinkResponse里面的objects 只有返回code 判断200是成功 |
| | | * |
| | | * @param msg |
| | | * @return |
| | | */ |
| | | public static <T> T convertLinkWithCodeResponse(Object msg, Type type) { |
| | | T bean = null; |
| | | if (msg != null && msg instanceof LinkResponse) { |
| | | LinkResponse linkResponse = (LinkResponse) msg; |
| | | String data = linkResponse.getData(); |
| | | if (!TextUtils.isEmpty(data)) { |
| | | try { |
| | | final BaseLocalWithCodeResponse<T> response = GsonConvert.getGson().fromJson(data, type); |
| | | if (response != null) { |
| | | bean = response.getCode(); |
| | | } |
| | | } catch (Exception e) { |
| | | LogUtils.e("convertLinkResponse catch:" + e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | return bean; |
| | | } |
| | | } |