【发布时间】:2020-11-05 23:39:06
【问题描述】:
我正在构建一个用于锻炼的跟踪应用程序。随着 gps 每 1 秒更新一次,纬度和经度被添加到一个数组中。在练习结束时,当您按下保存时,将执行以下方法,将所有坐标发送到数据库。因为它是一个异步请求,所以坐标不会以正确的顺序加载到数据库中。我该如何解决这个问题,所以它会等到循环的每次迭代完成或类似的事情。谢谢
/* 从 latitudeAndLongitudePoints ArrayList 中插入经纬度点 进入 db*/
中的 latitudeandlongitudepoints 表private void insertLatitudeAndLongitudePoints(){
//iterates though array of co-ordinates, and adds to database
for(int loop=0; loop<latitudeAndLongitudePoints.size();loop++) {
final int index = loop;
StringRequest stringRequest = new StringRequest(Request.Method.POST,
"http://rrush01.lampt.eeecs.qub.ac.uk/insertLatitudeAndLongitudePoints.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error: " + error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Latitude", String.valueOf(latitudeAndLongitudePoints.get(index).latitude));
params.put("Longitude", String.valueOf(latitudeAndLongitudePoints.get(index).longitude));
params.put("User", AccountInfo.accountEmail);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
}
【问题讨论】:
标签: java android android-studio http-post android-volley