I have a problem with my code that works very well for me, but when I intercept with a proxy it sends me a double request. I would like to solve that error and only send it once. I was checking the web and I found a similar question but I am new to this language and I don't know how to implement it.
Android Volley double post when have slow request
My code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText usernameEditText = findViewById(R.id.username);
final EditText passwordEditText = findViewById(R.id.pwd);
final Button loginButton = findViewById(R.id.btnLogin);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String URL = "https://www.myurl.com/login.php";
String username = usernameEditText.getText().toString();
String pwd = passwordEditText.getText().toString();
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject r = new JSONObject(response);
String status = r.getString("msg");
if (status.equals("OK")) {
Intent secretIntent = new Intent().setClass(LoginActivity.this, MenuActivity.class);
startActivity(secretIntent);
finish();
} else {
Toast.makeText(LoginActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("pwd", pwd);
return params;
}
};
requestQueue.add(stringRequest);
}
});
}