Get data from an Override method

1

I want to obtain any value of an Override method that is inside another method to be able to return a data and be able to make a comparison.

Within the method onResponse I want to obtain a data so that the method cargarDatos can return a value and make a comparison to execute an action:

 public void cargarDatos(final String est){
    RequestQueue queue = Volley.newRequestQueue(this);
    String url="";
    StringRequest js=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if(response.equals("[]")){
                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String fec=fmt.format(new Date());
                grabarMal(cod_per,cod_tra,fec,est);
                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(usuVerDatos.this);
                builder.setMessage("Inspector "+nom+" el código escaneado en "+tra+" no se encuentra registrado").setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent a=new Intent(getApplicationContext(),usuPrincipal.class);
                        startActivity(a);
                        finish();
                    }
                })
                        .setCancelable(false).show();

            }else {
                try {
                    JSONArray json=new JSONArray(response);

                  Estivador a=new Estivador(json.getJSONObject(0).getString("cod_est"),json.getJSONObject(0).getString("nom_est"),
                            json.getJSONObject(0).getString("ape_pat_est"),json.getJSONObject(0).getString("ape_mat_est"),
                            json.getJSONObject(0).getString("dni_est"), json.getJSONObject(0).getString("nom_aso"),json.getJSONObject(0).getString("fot_est"),json.getJSONObject(0).getString("cod_aso"));

                    String url="";


                    txtEstCodDatos.setText(a.getCod_est().toString());
                    txtEstNomDatos.setText(a.getNom_est().toString());
                    txtEstApePatDatos.setText(a.getApe_pat_est().toString());
                    txtEstApeMatDatos.setText(a.getApe_mat_est().toString());
                    txtEstDniDatos.setText(a.getDni_est().toString());
                    txtEstAsoDatos.setText(a.getNom_aso().toString());
                    cargarimagen(url+a.getFot_est().toString());

                    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String fec=fmt.format(new Date());
                    grabarCorrecto(cod_per,cod_tra,a.getCod_est().toString(),a.getCod_aso().toString(),fec);


                }catch (JSONException e){

                }
            }}
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(usuVerDatos.this,"Error en la conexión",Toast.LENGTH_LONG).show();
        }
    });
    queue.add(js);

}
    
asked by Jinex Velarde 17.09.2017 в 01:39
source

2 answers

1

Try creating a listener to run when you get the server's response. This is because if you want to return the server's response, you will have to make the request synchronic which in turn will force you to create Task to avoid using view controls in another thread.

Create an interface:

public interface DatosResponseListener
{
  void datosResponse(String datos);
}

Then in your method you define the listener that you will execute when you get the result:

public void cargarDatos(final String est, DatosResponseListener listener)
{
    RequestQueue queue = Volley.newRequestQueue(this);
    String url="";
    StringRequest js=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
         listener.datosResponse(response);
          //..

Then the use would be:

cargarDatos("est", new DatosResponseListener(){
  @Override
  public void datosResponse(String response)
  {
     // procesas la respuesta del servidor
  }
});
    
answered by 17.09.2017 / 03:29
source
0

Declare a variable in the method cargarDatos to which you assign the value you want in the method onResponse , then return that variable.

public String cargarDatos(final String est){

    // variable en la que almacenaras el valor que quieres obtener de onResponse
    String obtenerValor = "";

    RequestQueue queue = Volley.newRequestQueue(this);
    String url="";
    StringRequest js=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            // Aqui le asignas el valor que quieres a la variable.
            obtenerValor = response;

            if(response.equals("[]")){
                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String fec=fmt.format(new Date());
                grabarMal(cod_per,cod_tra,fec,est);
                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(usuVerDatos.this);
                builder.setMessage("Inspector "+nom+" el código escaneado en "+tra+" no se encuentra registrado").setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                         Intent a=new Intent(getApplicationContext(),usuPrincipal.class);
                         startActivity(a);
                         finish();
                    }
                })
                .setCancelable(false).show();

            }else {
                try {
                    JSONArray json=new JSONArray(response);

                  Estivador a=new Estivador(json.getJSONObject(0).getString("cod_est"),json.getJSONObject(0).getString("nom_est"),
                        json.getJSONObject(0).getString("ape_pat_est"),json.getJSONObject(0).getString("ape_mat_est"),
                        json.getJSONObject(0).getString("dni_est"), json.getJSONObject(0).getString("nom_aso"),json.getJSONObject(0).getString("fot_est"),json.getJSONObject(0).getString("cod_aso"));

                    String url="";


                    txtEstCodDatos.setText(a.getCod_est().toString());
                    txtEstNomDatos.setText(a.getNom_est().toString());
                    txtEstApePatDatos.setText(a.getApe_pat_est().toString());
                    txtEstApeMatDatos.setText(a.getApe_mat_est().toString());
                    txtEstDniDatos.setText(a.getDni_est().toString());
                    txtEstAsoDatos.setText(a.getNom_aso().toString());
                    cargarimagen(url+a.getFot_est().toString());

                    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String fec=fmt.format(new Date());
                    grabarCorrecto(cod_per,cod_tra,a.getCod_est().toString(),a.getCod_aso().toString(),fec);


                }catch (JSONException e){

                }
            }}
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(usuVerDatos.this,"Error en la conexión",Toast.LENGTH_LONG).show();
            }
        });
    queue.add(js);

    // retornas el valor que le asignaste a la variable 
    return obtenerValor;

}
    
answered by 17.09.2017 в 02:34