Android 等待 volley 完成并稍后更新 UI
Android wait for volley to finish and update UI later
我正在使用 volley 包从网站检索数据 (JSON)。
这是我的方法
private void getEarthquakeList(){
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
//Earthquake Feeder
String url ="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response is: ",response);
//Parsing Json
parseJson(response);
final ListView earthquakeListView = (ListView)findViewById(R.id.list);
//Sort the array according to magnitude
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error",error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
问题是,现在我正在更新此方法中的 UI 之后 returns 响应。
这些是行
//Sort the array according to a date
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
当我在 MainActivity 中 运行 时没有问题,用户打开应用程序并获取地震列表。
当我想切换到我每隔几分钟监控一次的服务时,或者当网站内容发生变化时,问题就开始了。
所以我想要我的方法而不更新其中的 UI 。
问题是,如果我不更新 onResponse 内的 UI,UI 线程将继续并导致一个空数组。
所以这个数组保持空 earthquakeArrayList
如果我不在里面做它
public void onResponse(String response)
想法如何将两者分开,一方面 运行 方法和获取数据,另一方面主线程将能够访问数据而不是完成执行。
谢谢
EG
目前推荐的解决方案是使用LiveData. LiveData works on the Observer Pattern,这样你可以在服务和Activity中让任意数量的观察者观察更新的数据,但他们需要在同一个语境。这些是有点高级的主题,但却是将 UI 与数据层分离的好方法。你可以通过这两个链接
我正在使用 volley 包从网站检索数据 (JSON)。
这是我的方法
private void getEarthquakeList(){
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
//Earthquake Feeder
String url ="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Response is: ",response);
//Parsing Json
parseJson(response);
final ListView earthquakeListView = (ListView)findViewById(R.id.list);
//Sort the array according to magnitude
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error",error.getMessage());
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
问题是,现在我正在更新此方法中的 UI 之后 returns 响应。
这些是行
//Sort the array according to a date
earthquakeArrayList.sort((a, b) -> Double.compare(b.getTime(), a.getTime()));
mAdapter = new EarthquakeAdapter(getApplicationContext(), earthquakeArrayList);
earthquakeListView.setAdapter(mAdapter);
当我在 MainActivity 中 运行 时没有问题,用户打开应用程序并获取地震列表。
当我想切换到我每隔几分钟监控一次的服务时,或者当网站内容发生变化时,问题就开始了。 所以我想要我的方法而不更新其中的 UI 。 问题是,如果我不更新 onResponse 内的 UI,UI 线程将继续并导致一个空数组。
所以这个数组保持空 earthquakeArrayList
如果我不在里面做它
public void onResponse(String response)
想法如何将两者分开,一方面 运行 方法和获取数据,另一方面主线程将能够访问数据而不是完成执行。 谢谢 EG
目前推荐的解决方案是使用LiveData. LiveData works on the Observer Pattern,这样你可以在服务和Activity中让任意数量的观察者观察更新的数据,但他们需要在同一个语境。这些是有点高级的主题,但却是将 UI 与数据层分离的好方法。你可以通过这两个链接