我尝试用Android volley sending data twice和Android volley is sending information twice with image upload之类的帖子来解决我的问题,但是Volley
每次单击“保存”按钮时都会不断发送信息。有时连接速度可能很慢,用户可以继续单击“保存”按钮,所以我试图阻止这种情况的发生-信息应该只发送一次。
我该怎么做?
这是我的“保存”按钮中的代码:
//When the user clicks save
StringRequest stringRequest = new StringRequest(Request.Method.POST, NewContact_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(NewContact.this, "Saved", Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
//post these details to the php file
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//post the phone number to php to get the user_id in the user table
params.put("phonenumberofuser", phoneNoofUserCheck);
//the second value, categoryname.getText().toString() etc...
// is the value we get from Android.
//the key is "category", "name" etc.
// When we see these in our php, $_POST["category"],
//put in the value from Android
params.put("category", categoryname.getText().toString());
params.put("name", namename.getText().toString());
params.put("phone", phonename.getText().toString());
params.put("address", addressname.getText().toString());
params.put("comment", commentname.getText().toString());
params.put("public_or_private", String.valueOf(public_or_private));
System.out.println("public_or_private is " + String.valueOf(public_or_private));
//this is the JSON Array of checked contacts
//it will be of the form
//[{"checkedContact":"+3531234567"},{"checkedContact":"+353868132813"}]
params.put("checkedContacts", checkedContacts.toString());
return params;
}
};
stringRequest.setRetryPolicy(new
DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, 1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest);
}