'How to Get Value from JSON in Firebase Remote Config

I am a novice to Android app development and Firebase.

I want to know how can I get the value (String & Int) in the JSONArray file stored in Firebase Remote Config?

I use Firebase Remote Config with the final goal to compare my app's version code and priority level with the one stored in Firebase Remote Config to determine initiation of App Update notification, but so far I still unable get Remote Config value.

I tried to parse the JSON using Volley (jsonParse in MainActivity2 class), but it also didn't work. (Error bad url)

I have several times tried to implement previous answers, but maybe because of my misunderstanding, all those came to no avail.

Can I declare an array to Firebase Remote config?

Can I get JSONObject from Default value of Firebase Remote Config

FirebaseRemoteConfig getString is empty

I also have read this interesting article about implementing in-app updates notifications with some specific criteria using Remote Config, but unfortunately for me, the codes are written in Kotlin.

https://engineering.q42.nl/android-in-app-updates/

test_json file stored in Firebase Remote Config.

[
  {
    "versionCode": "1",
    "priorityLevel": 2
  },
  {
    "versionCode": "2",
    "priorityLevel": 4
  },
  {
    "versionCode": "3",
    "priorityLevel": 1
  }
]

MainActivity2 class

        remoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setFetchTimeoutInSeconds(2000)
                .build();
        remoteConfig.setConfigSettingsAsync(configSettings);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                remoteConfig.fetchAndActivate().addOnCompleteListener(new OnCompleteListener<Boolean>() {
                    @Override
                    public void onComplete(@NonNull Task<Boolean> task) {
                        if (task.isSuccessful()) {
                            String object = remoteConfig.getString("test_json");
                            //jsonParse(object);
                            Gson gson = new GsonBuilder().create();
                            ArrayList<Lessons> lessons = gson.fromJson(object, 
                                 new TypeToken<List<Lessons>>(){}.getType());

                        } else {
                            Toast.makeText(MainActivity2.this, "Fetch Failed!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

        private void jsonParse(String object) {
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, object, null,
                    new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("condition");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject condition = jsonArray.getJSONObject(i);

                                String versionCode = condition.getString("versionCode");
                                int priorityLevel = condition.getInt("priorityLevel");

                                textView.append(versionCode + "\n\n");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mQueue.add(request);
    }

Lessons class

public class Lessons {
    String versionCode;
    Int priorityLevel;
}

Any help would be greatly appreciated.

Thank you.



Solution 1:[1]

I recommend you use Gson.

data class Lessons(
    val versionCode: String? = null,
    val priorityLevel: Int? = null
)

and then, use getValue() or getString() to get the data.

val list = Gson().fromJson(getValue("test_json").asString(), Array<Lessons>::class.java)

In my case, getString() also worked.

val list = Gson().fromJson(getString("test_json"), Array<Lessons>::class.java)

This is simpler.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 c-an