Gson解析空字符串异常的处理

面对一些不规范的json,我们的gson解析经常会抛出各种异常导致app崩溃,这里可以采取一些措施来避免

Json异常情况

先来看一个后台返回的json

正常情况下json:

  1. {
  2. "code":0,
  3. "msg":"ok",
  4. "data":{
  5. "id":5638,
  6. "newsId":5638
  7. }
  8. }
复制代码

data部分对应的实体类:

  1. public class JsonBean {
  2. private int id;
  3. private int newsId;
  4. public int getId() {
  5. return id;
  6. }
  7. public void setId(int id) {
  8. this.id = id;
  9. }
  10. public int getNewsId() {
  11. return newsId;
  12. }
  13. public void setNewsId(int newsId) {
  14. this.newsId = newsId;
  15. }
  16. }
复制代码

异常情况json(后台数据库newsId字段未查询到对应数据):

  1. {
  2. "code":0,
  3. "msg":"ok",
  4. "data":{
  5. "id":5638,
  6. "newsId":""
  7. }
  8. }
复制代码

这样Gson在解析时就会抛出解析错误的异常,app崩溃,原因是无法将""转化为int

json异常的处理

我们期望在后台返回的json异常时,也能解析成功,空值对应的转换为默认值,如:newsId=0;

这里排除掉后台开发人员输出时给你做矫正,还是得靠自己啊---

我们写一个针对int值的类型转换器,需要实现Gson的 JsonSerializer 接口和 JsonDeserializer ,即序列化和反序列化接口

  1. public class IntegerDefault0Adapter implements JsonSerializer<Integer>, JsonDeserializer<Integer> {
  2. @Override
  3. public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  4. throws JsonParseException {
  5. try {
  6. if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为int类型,如果后台返回""或者null,则返回0
  7. return 0;
  8. }
  9. } catch (Exception ignore) {
  10. }
  11. try {
  12. return json.getAsInt();
  13. } catch (NumberFormatException e) {
  14. throw new JsonSyntaxException(e);
  15. }
  16. }
  17. @Override
  18. public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
  19. return new JsonPrimitive(src);
  20. }
  21. }
复制代码
同理Long及Double类型

double=>

  1. public class DoubleDefault0Adapter implements JsonSerializer<Double>, JsonDeserializer<Double> {
  2. @Override
  3. public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
  4. try {
  5. if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00
  6. return 0.00;
  7. }
  8. } catch (Exception ignore) {
  9. }
  10. try {
  11. return json.getAsDouble();
  12. } catch (NumberFormatException e) {
  13. throw new JsonSyntaxException(e);
  14. }
  15. }
  16. @Override
  17. public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
  18. return new JsonPrimitive(src);
  19. }
  20. }
复制代码

long=>

  1. public class LongDefault0Adapter implements JsonSerializer<Long>, JsonDeserializer<Long> {
  2. @Override
  3. public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
  4. throws JsonParseException {
  5. try {
  6. if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为long类型,如果后台返回""或者null,则返回0
  7. return 0l;
  8. }
  9. } catch (Exception ignore) {
  10. }
  11. try {
  12. return json.getAsLong();
  13. } catch (NumberFormatException e) {
  14. throw new JsonSyntaxException(e);
  15. }
  16. }
  17. @Override
  18. public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
  19. return new JsonPrimitive(src);
  20. }
  21. }
复制代码

所以使用是这样的:北京整形医院http://www.bj-swjtu.com/

  1. return new Retrofit.Builder()
  2. .client(okHttpClient)//设置网络访问框架
  3. .addConverterFactory(GsonConverterFactory.create(buildGson()))//添加json转换框架
  4. .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//让Retrofit支持RxJava
  5. .baseUrl(baseUrl)
  6. .build();
  7. /**
  8. * 增加后台返回""和"null"的处理
  9. * 1.int=>0
  10. * 2.double=>0.00
  11. * 3.long=>0L
  12. *
  13. * @return
  14. */
  15. public static Gson buildGson() {
  16. if (gson == null) {
  17. gson = new GsonBuilder()
  18. .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
  19. .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
  20. .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
  21. .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
  22. .registerTypeAdapter(Long.class, new LongDefault0Adapter())
  23. .registerTypeAdapter(long.class, new LongDefault0Adapter())
  24. .create();
  25. }
  26. return gson;
  27. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值