开发者

Help with JSON error handling and google translate api

开发者 https://www.devze.com 2023-02-24 23:52 出处:网络
I\'m currently parsing a JSON response from the Google Translate API.I\'m able to do this with no problem. Seeing as how I don\'t have a lot of XML experience (I\'m more of an XML guy), I\'m having tr

I'm currently parsing a JSON response from the Google Translate API. I'm able to do this with no problem. Seeing as how I don't have a lot of XML experience (I'm more of an XML guy), I'm having trouble figuring out how to implement some error handling in my JSON parsing. I am using the JSON j2me library.

Here is a successful response:

{"responseData": {"translatedText":"Teks te vertaal ...","detectedSourceLanguage":"en"}, "responseDetails": null, "responseStatus": 200}

And here is an unsuccessful response:

{"responseData": null, "responseDetails": "could not reliably detect source language", "responseStatus": 400}

So, if the translation is unsuccessful, I want to put the value of "responseDetails" into a string. Here is my parsing code, which is not currently parsing out responseDetails correctly. Instead, the "catch" of the "try" is being caught.

try {
            JSONObject responseObject = new JSONObject(response);
            if (responseObject != null) {
                JSONObject responseData = responseObj开发者_运维技巧ect
                        .getJSONObject("responseData");
                if (responseData != null) {
                    String translatedText = responseData
                            .getString("translatedText");
                    Notify.alert(translatedText);
                } else {
                    String responseDetails = responseObject
                            .getString("responseDetails");
                    Notify.alert(responseDetails);
                }
            }
        } catch (Exception e) {
            Notify.alert("Unable to translate!");
        }

Can anyone see where I'm going wrong?

Thanks!


Since you say the catch block is being triggered, I'd start debugging by looking at what Exception is being thrown. You could simply append your alert string to include e.toString().

So change your alert in the catch block to be:

Notify.alert("Unable to translate! " + e.toString());

And see what the actual error that's being thrown is.

Based on your comment, yes it looks like it's trying to create a JSONObject on a null value, so nest another try/catch block and parse it accordingly that way.

try {
    JSONObject responseObject = new JSONObject(response);
    if (responseObject != null) {
        /* Try create a new JSON object from the 
         * responseData object. If it fails, 
         * display an alert */
        try {
            JSONObject responseData = responseObject
                    .getJSONObject("responseData");

            if (responseData != null) {
                String translatedText = responseData
                        .getString("translatedText");
                Notify.alert(translatedText);
            } 

        } catch (Exception e) {
            String responseDetails = responseObject
                    .getString("responseDetails");
            Notify.alert(responseDetails);
        }
    }
} catch (Exception e) {
    Notify.alert("Unable to translate outer block!");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消