How to parse the Search JSON results and show them in a list, in Android, Java

You can review the documentation for how to get the Search result:
https://developer.oxforddictionaries.com/documentation

The return string is in JSON format.

and this is how to get the results in Android/Java.

// str is the string from the Search API. I limited the results to 20 requests only.
// Otherwise, the default is 5000 requests, which is very huge for the app.

private void ExtractSuggestions (String str) {
// Spannable is necessary for creating links in editText
SpannableStringBuilder spnBuilder = new SpannableStringBuilder();
EditTextView = (TextView) findViewById(R.id.editText);
EditTextView.setText("");
spnBuilder.clearSpans();
spnBuilder.clear();
int before;
JSONObject obj = null;
try {
// Converting the String to JSONObject
// try-catch block is necessary for all JSON objects, arrays, strings, keys, tokens…
obj = new JSONObject(str);
} catch (JSONException ignored) {
}

    JSONArray myJsonArray = null;

    try {
    // getting the first JSONArray: results
        myJsonArray = obj.getJSONArray("results");
    } catch (JSONException ignored) {
    }
    if (myJsonArray != null) {
        int myJsonArrayLength = myJsonArray.length();
    // Put the word: Suggestions in the top of the list

            before = spnBuilder.length();
            spnBuilder.append("Suggestions\n\n");
            spnBuilder.setSpan(new RelativeSizeSpan(2.5f), before, before + 11, Spanned.SPAN_ EXCLUSIVE_EXCLUSIVE);
            spnBuilder.setSpan(new ForegroundColorSpan(Color.RED), before, before + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        for (int d = 0; d < myJsonArrayLength; d++) {
            String temp1 = "";
            String temp2 = "";


            try {
    //getting the suggestion and the region
    // Suggestion request are a mixed list between British Oxford and American Oxford.

//Therefore, it is necessary to get the region along with the word
temp1 = myJsonArray.getJSONObject(d).getString("word").toLowerCase();
temp2 = myJsonArray.getJSONObject(d).getString("region").toLowerCase();
} catch (JSONException ignored) {
}

// word and region should be declared final, because the inner class ClickableSpan will use them    
            final String word = temp1;
            final String region = temp2;

// create a clickable link out of every suggestion
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View view) {
        //when the suggestion is clicked, go to fetch the definition
                    new CallbackTask().execute(dictionaryEntries(word, 1, region));
                }
            };

// put  the suggestion in the Spannable
            before = spnBuilder.length();
            spnBuilder.append(word + "\n\n");
            spnBuilder.setSpan(new RelativeSizeSpan(1.5f), before, before + word.length(), Spanned.SPAN_ EXCLUSIVE_EXCLUSIVE );
            spnBuilder.setSpan(new ForegroundColorSpan(Color.BLUE), before, before + word.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spnBuilder.setSpan(clickableSpan, before, before + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        }

    }
    spnBuilder.append("\n\n\n\n");   // to put some empty space in the end of the list
    // setMovementMethod  is necessary to create links

EditTextView.setMovementMethod(LinkMovementMethod.getInstance());
// Show the clickable list
EditTextView.setText(spnBuilder, TextView.BufferType.SPANNABLE);
}

Comments

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin
    edited August 2017

    Thanks @Developer,

    I'm sure there will be people browsing, who will find this useful (and also probably some smart alec, who thinks they can do it better! :tongue: )

Sign In or Register to comment.