-1

I am trying to process a JSON-format string in Android. Does not work. To troubleshoot I use toasts. Now I have two toasts, but only one is showing. Android Studio (newest version) does not show me any issues. Build works fine.

See the code:

    if (shpref_url == "no URL defined")
    {
        Toast toast2 = Toast.makeText(getApplicationContext(), getString(R.string.txturljson), Toast.LENGTH_LONG);
        toast2.show();
        try {
            JSONArray txturljson = new JSONArray(getString(R.string.txturljson));
            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.txturljson), Toast.LENGTH_LONG);
            toast.show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

So my question: Why does it show only one toast?

Thanks, Joachim

Hermes
  • 2,828
  • 2
  • 14
  • 34
josewe
  • 51
  • 6
  • 1
    You are using same text for both toast, user won't be able to tell the difference between the two , and also time gap between two seems to be almost 0, both will show almost simultaneously – Nitish Sep 28 '21 at 06:39
  • use logs for better debugging! – Mohak Shah Sep 28 '21 at 06:51

1 Answers1

0

Ok, I got it.

Seeing this post: quite simular JSONArray string issue I got that the string to be parsed must not contain this -> " character, which was the case in my string getString(R.string.txturljson).

Therefore I had to replace '"' with '\"' in my string.

In other words, this:

{"key1": "value1", "key2": "value2"}

should look like this:

{\"key1\": \"value1\", \"key2\": \"value2\"}

if you store a JSON Object or JSON Array in strings.xml.

Now both toasts are shown...

josewe
  • 51
  • 6