簡體   English   中英

如何在Android / java中將GoogleJsonResponseException轉換為JSONObject?

[英]How to convert a GoogleJsonResponseException into a JSONObject in android/java?

在我的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? 還是應該手動解析字符串?

謝謝

JSONObject具有接受字符串的構造函數。 你有嘗試過嗎?

否則,請閱讀文檔

j.getDetails().getMessage()

而且由於不清楚您想要哪個消息字段,因此這是嵌套的消息字段。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM