安卓工具类
安卓开发工具类
GsonUtils
/** * 基于Gson封装的JSON解析/反解析工具类 * 参考: * http://blog.csdn.net/chenliguan/article/details/48623283 * http://www.cnblogs.com/top5/archive/2012/02/03/2336493.html */ public class GsonUtils { private static Gson gson; static { GsonBuilder gsonBuilder = new GsonBuilder(); // gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss"); gson = gsonBuilder.create(); } /*** * 将List对象序列化为JSON文本 */ public static <T> String toJSONString(List<T> list) { return gson.toJson(list); } /*** * 将对象序列化为JSON文本 * @param object * @return */ public static String toJSONString(Object object) { return gson.toJson(object); } /*** * Json对象解析 * @return */ public static <T> T toBean(String str,Class<T> classOfT) { return new Gson().fromJson(str, classOfT); } /*** * Json List对象解析 * @return */ public static <T> List<T> toBeanList(String str,Type listType) { // Type listType = new TypeToken<ArrayList<T>>() {}.getType(); return new Gson().fromJson(str, listType); } /** * 转成list * 解决泛型问题 * @param json * @param cls * @param <T> * @return */ public <T> List<T> toList(String json, Class<T> cls) { Gson gson = new Gson(); List<T> list = new ArrayList<T>(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); for(final JsonElement elem : array){ list.add(gson.fromJson(elem, cls)); } return list; } }
ObjectTools
/** * Created by x5670 on 2017/11/13. * ObjectTools * https://www.cnblogs.com/hrlnw/p/3617478.html */ public class ObjectTools { /** * Read object form base64 string * Object save in string type instance * http://blog.csdn.net/dangnianmingyue_gg/article/details/47373699 * * @param objStr * @return */ public static Object readObjectFromBase64Str(String objStr) { Object temp = null; try { byte[] bytes = base64String2Byte(objStr); ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ObjectInputStream objIn = new ObjectInputStream(inputStream); temp = objIn.readObject(); objIn.close(); } catch (Exception ex) { ex.printStackTrace(); } return temp; } /** * Get object base64 string * http://blog.csdn.net/z69183787/article/details/16883243 * http://longpo.iteye.com/blog/2203627 * http://blog.csdn.net/maoyeqiu/article/details/45748717 * * @param object * @return */ public static String getBase64ObjectString(Object object) { ByteArrayOutputStream aos = new ByteArrayOutputStream(); try { ObjectOutputStream objOut = new ObjectOutputStream(aos); objOut.writeObject(object); objOut.flush(); byte[] bytes = aos.toByteArray(); objOut.close(); String res = byte2Base64String(bytes); return res; } catch (Exception e) { e.printStackTrace(); } return null; } /** * base64字符串转byte[] * http://blog.csdn.net/big1989wmf/article/details/70144803 * http://blog.csdn.net/chenzhou121/article/details/30756363 * @param base64Str * @return */ public static byte[] base64String2Byte(String base64Str){ return Base64.decode(base64Str,Base64.DEFAULT); } /** * byte[]转base64 * @param byteArr * @return */ public static String byte2Base64String(byte[] byteArr){ return Base64.encodeToString(byteArr,Base64.DEFAULT); } /** * Write object to file * * @param obj * @param path */ public static void writeObjectToFile(Object obj, String path) { File file = new File(path); FileOutputStream out; try { out = new FileOutputStream(file); ObjectOutputStream objOut = new ObjectOutputStream(out); objOut.writeObject(obj); objOut.flush(); objOut.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Read Object form file * * @param path * @return */ public static Object readObjectFromFile(String path) { Object temp = null; File file = new File(path); FileInputStream in; try { in = new FileInputStream(file); ObjectInputStream objIn = new ObjectInputStream(in); temp = objIn.readObject(); objIn.close(); } catch (Exception e) { e.printStackTrace(); } return temp; } }