Post request on volley not sending parameters on first click

1k Views Asked by At

Im making an app in android studio and im using volley library to make post requests on the click of a button.

The first time i press the button the response is always null, but the second time it works properly.

Here's the code:

public class LoginActivity extends Activity{
Button btnEnviar;
EditText domn;
EditText user;
EditText pass;
String respuesta=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);


    btnEnviar = (Button)findViewById(R.id.btnLogin);
    domn = (EditText)findViewById(R.id.txtDominio);
    user = (EditText)findViewById(R.id.txtUser);
    pass = (EditText)findViewById(R.id.txtPassword);
    btnEnviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String dominio = domn.getText().toString();
            String auth=loginRequest(("server url"));

            Log.i("Server response:", "" + auth);


            /*Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);*/
        }
    });
}

public String loginRequest(String url){
    ;
    StringRequest request = new StringRequest(Request.Method.POST,url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    respuesta = s;
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError ex) {
                   // System.out.println(ex.getMessage().toString());
                }
            }
    ){
        @Override
        protected Map<String,String> getParams(){
            String usr = user.getText().toString();
            String psw = pass.getText().toString();
            Map<String,String> params = new HashMap<String, String>();
            params.put("User",usr);
            params.put("Pass",psw);
            params.put("DeviceName","Nombre");
            params.put("DeviceType","Tipo");
            params.put("OSVersion","Version");
            params.put("VendorID","ID");
            Log.i("USER:",usr);
            Log.i("PASS:", psw);

            return params;
    }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }
    };

    RequestQueue queue = Volley.newRequestQueue(this);
    queue.add(request);
    return respuesta;
}

Also im getting this log responses:

On first click:

06-16 10:09:33.325  14628-14628/? I/Server response:﹕ null
06-16 10:09:33.375  14628-14744/? I/USER:﹕ username
06-16 10:09:33.375  14628-14744/? I/PASS:﹕ password

On second click:

06-16 10:09:48.945  14628-14628/? I/Server response:﹕ 1
TOKEN
06-16 10:09:48.955  14628-14759/? I/USER:﹕ username
06-16 10:09:48.955  14628-14759/? I/PASS:﹕ password

Any idea on how to fix it? Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Your return String respuesta is set asynchronously.

The I/Server response:﹕ 1 that you're seeing is actually the response that you're getting back from the first button press.

You should move your log into the onResponse

public void onResponse(String s) {
    respuesta = s;
    //todo add log and logic here
}