王学岗生成泛型的简易Builder

github大佬地址
使用

//class 可以传参DataBean.class
 public static  T handlerJson(String json, Class tClass) {
        T resultData = null;
        if (CommonUtils.StringNotNull(json) && !nullString.equals(json)) {
            if (isArray(json)) {
                resultData = BaseNetUtil.parseFromJson(json,
                        TypeBuilder.newInstance(ArrayList.class).addTypeParam(tClass).build());
            } else if (isObj(json)) {
                resultData = BaseNetUtil.parseFromJson(json, TypeBuilder.newInstance(tClass).build());
            }
        }
        return resultData;
    }
  public ResultData parseNetworkResponse(Response response, int id) throws Exception {
  //s 是JsonObject
 ResultData resultData = BaseNetUtil.parseFromJson(s.toString(), new TypeToken>() {
            }.getType());
            }
    public static  T parseFromJson(String json, Type type) {
        try {
            return !TextUtils.isEmpty(json) && type != null ? (T) new Gson().fromJson(json, type) : null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

你可能感兴趣的:(随笔,android)