【问题标题】:How to convert a GoogleJsonResponseException into a JSONObject in android/java?如何在 android/java 中将 GoogleJsonResponseException 转换为 JSONObject?
【发布时间】:2016-12-21 01:01:48
【问题描述】:

在我的 android 应用程序中,我正在处理 GoogleJsonResponseException,根据它的 ResultCode 和消息,我应该有不同的代码。

try {
            //type method here

        } catch (GoogleJsonResponseException j) {


            int satusCode = j.getStatusCode();
            String message = getMessageCode(j.toString());}      

其中 getMessage(String s) 是一个以这种 String 形式返回异常的方法:

{
"code": 409,
"errors": [
    {
        "domain": "global",
        "message": "Error message",
        "reason": "conflict"
    }
],
"message": "Error message"}

我需要“消息”中的数据。那么有没有办法可以将此字符串更改为 JSONObject 或将 GoogleJsonResponse 异常从开头转换为 JSONObject?还是我应该手动解析字符串?

谢谢

【问题讨论】:

    标签: java android exception-handling jsonobject jsonobjectrequest


    【解决方案1】:

    JSONObject 有一个接受字符串的构造函数。你试过吗?

    要不然reading the documentation我明白了

    j.getDetails().getMessage()
    

    由于不清楚您想要哪个消息字段,这里是嵌套的。

    j.getDetails().getErrors().get(0).getMessage();
    

    【讨论】:

    • 是的,构造函数在我的情况下不起作用,但您的第二个建议有效,谢谢!需要注意的是,我已经尝试过 j.getMessage() 但这会以我在上面发布的 json 形式重新调整字符串,这就是我没有使用它的原因。
    猜你喜欢
    • 2011-07-11
    • 2020-01-12
    • 2018-01-06
    • 1970-01-01
    • 2020-11-30
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    相关资源
    最近更新 更多