How to make me return the variable with the assigned values within a TRY

2
public Usuario buscar(final Usuario nombreUsuario) {

    Log.i("Usuario:", nombreUsuario.toString());
    final Usuario usuario = new Usuario();

    final String url = EstructuraURL.URL_LOGIN;

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {

                JSONObject jsonObject = new JSONObject(response);
                String success = jsonObject.getString("success");

                usuario.setId(jsonObject.getInt("id"));
                usuario.setEmail(jsonObject.getString("nombreUsuario"));
                usuario.setNombre(jsonObject.getString("nombre"));

                Log.i("Respuesta", response);

                if (success.equals("1")) {

                    Toast.makeText(context, usuario.toString(), Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();

                Toast.makeText(context, "Error!" + e.toString(), Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("Respueta", error.toString());
            Toast.makeText(context, "Error!" +error.toString(), Toast.LENGTH_SHORT).show();

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();

            params.put("nombreUsuario", nombreUsuario.getEmail());
            params.put("password", nombreUsuario.getPassword());

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(context);
    requestQueue.add(stringRequest);

    return null;
}

Here the code, if at the end I put user return; the values they give me are all null ...

    
asked by Ivan De Js Matrille 31.12.2018 в 04:29
source

1 answer

0

The problem you have is that the calls are asynchronous, following a pattern similar to an AJAX call, so you can not return anything:

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //... código que se ejecuta cuando tenemos la respuesta
            }
        }, 
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //... código que se ejecuta en caso de error
            }
        }
) {
    //... código extendiendo la clase StringRequest
};

This whole code builds the call and assigns a value to stringRequest, but does not wait for the response before moving on to the next line of code.

The answer will come at some point and will lead to the execution of the onResponse method. Instead of returning the value, you can create a method in your class that receives as a parameter a User instance and call this method from the onResponse method:

public void buscar(final Usuario nombreUsuario, MiClaseConMetodo mccm) {

    Log.i("Usuario:", nombreUsuario.toString());
    final Usuario usuario = new Usuario();

    final String url = EstructuraURL.URL_LOGIN;

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,

        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //... código que se ejecuta cuando tenemos la respuesta
                mccm.tengoUsuario(usuario);
            }
        },
        ...
    
answered by 31.12.2018 / 12:41
source