Ged*_*eng 5 java android json firebase firebase-remote-config
我是 Android 应用开发和 Firebase 的新手。
我想知道如何获取存储在 Firebase 远程配置中的 JSONArray 文件中的值(String 和 Int)。
我使用 Firebase Remote Config 的最终目标是将我的应用程序的版本代码和优先级与 Firebase Remote Config 中存储的版本代码和优先级进行比较,以确定应用程序更新通知的启动,但到目前为止我仍然无法获取 Remote Config 值。
我尝试使用 Volley(MainActivity2 类中的 jsonParse)解析 JSON,但它也不起作用。(错误网址错误)
我曾多次尝试实施以前的答案,但也许由于我的误解,所有这些都无济于事。
我可以从 Firebase 远程配置的默认值获取 JSONObject
FirebaseRemoteConfig getString 为空
我还阅读了这篇有趣的文章,介绍如何使用远程配置按照某些特定标准实现应用内更新通知,但不幸的是,这些代码是用 Kotlin 编写的。
https://engineering.q42.nl/android-in-app-updates/
test_json
文件存储在 Firebase 远程配置中。
[
{
"versionCode": "1",
"priorityLevel": 2
},
{
"versionCode": "2",
"priorityLevel": 4
},
{
"versionCode": "3",
"priorityLevel": 1
}
]
Run Code Online (Sandbox Code Playgroud)
MainActivity2
班级
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);
}
Run Code Online (Sandbox Code Playgroud)
Lessons
班级
public class Lessons {
String versionCode;
Int priorityLevel;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
谢谢。
对我来说就是这样工作的
import com.google.gson.annotations.SerializedName
data class VersionData(
@SerializedName("name")
val name: String,
@SerializedName("age")
var age: Int,
@SerializedName("isFemale")
var isFemale: Boolean
)
init {
remoteConfig.fetchAndActivate()
}
private val _mutableLiveData = MutableLiveData<VersionData>()
val remoteLiveData: LiveData<VersionData> = _mutableLiveData
fun remoteConfiguration() {
val gson = Gson()
val remote = remoteConfig.fetchAndActivate()
remote.addOnSuccessListener{
val stringJson = remoteConfig.getString("json_object_name")
if(stringJson.isNotEmpty()){
val jsonModel = gson.fromJson(stringJson, VersionData::class.java)
_mutableLiveData.value = VersionData(
name = jsonModel.name,
age = jsonModel.age,
isFemale = jsonModel.isFemale
)
}else{
// probably your remote param not exists
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以在撰写中观察
val localLifecycle = LocalLifecycleOwner.current
myViewModel.remoteLiveData.observe(localLifecycle){
Log.d("remoteLiveData", "remoteLiveData: $it")
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8940 次 |
最近记录: |