【问题标题】:How to parse the error message in Throwable in Android如何在 Android 中解析 Throwable 中的错误消息
【发布时间】:2018-01-22 15:31:19
【问题描述】:

我从 Android 中的 Throwable 获得错误消息字符串(ex.getMessage()),如下所示

{"message":"此请求的授权已被拒绝。"}

从该错误消息字符串中仅获取消息部分的最佳方法是什么?

【问题讨论】:

    标签: android parsing throwable


    【解决方案1】:

    使用像 Gson 这样的 JSON 解析器并像这样创建类 ErrorMessage

    public class ErrorMessage{
    
    private String message;
    
    public getMessage(){
        return message;
        }
    }
    

    然后你只需使用 Gson 解析它:

    Gson gson = new Gson(); String error = gson.fromJson(jsonString, ErrorMessage.class);

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2017-04-23
      • 2014-12-31
      • 1970-01-01
      相关资源
      最近更新 更多