Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4271220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:24:45+00:00 2026-05-21T07:24:45+00:00

I’m currently parsing a JSON response from the Google Translate API. I’m able to

  • 0

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 = responseObject
                        .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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:24:46+00:00Added an answer on May 21, 2026 at 7:24 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.