新程序员。问题在底部。
我有一系列与车辆相对应的车队。我想用我的队列数组中的每个插槽连续调用服务器。
我希望使用 AsyncTask 来完成此操作。
private class refreshTruckInformation extends AsyncTask<URL, Void, Void> {
@Override
protected void doInBackground(URL... urls) {
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(Void... voids) {
}
}
**如何传入我的舰队数组以便我可以在 doInBackground 中使用它们?
我还希望有一个基于其所经历的舰队百分比的进度条。有什么好的方法可以做到这一点?**
谢谢!
最佳答案
**How do I pass in my array of fleets so that I can use them in my doInBackground?
请记住,refreshTruckInformation 仍然是一个类。因此,您可以使用任何构造函数或 settier 方法来传递数组。
I also want to have a progress bar that goes based on the percentage of fleets it has gone through. What is a good way to do this?**
在 doInBackground 方法中,您可以使用 publishProgress
方法发布进度。此进度参数将在 onProgressUpdate
方法中捕获
例如
private class RefreshTruckInformation extends AsyncTask<URL, Void, Void> {
private int[] b;
public RefreshTruckInformation (int[] a){
// use array
b = a;
}
public void setArray(int[] a){
// use array
b = a;
}
@Override
protected void doInBackground(URL... urls) {
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
@Override
protected void onPostExecute(Void... voids) {
}
}
并使用调用
RefreshTruckInformation r = new RefreshTruckInformation (yourArray);
或者调用类似的方法
r.setArray(yourArray);
并像这样执行
r.execute();
关于java - 了解 AsyncTask 与 Web 服务器访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17278987/