Place StringRequest / JSONRequest of Volley within a class to be called

3

I would like to ask about how to place a StringRequest inside a class, because it usually shows an error showing that it needs a Context to be able to work. I do not seem to ask for a Context context as a parameter. To be able to call a class that does the work and return a JSON, in order to work it, without having to repeat code.

StringRequest sRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String respuesta) {
                    //Toast.makeText(MainActivity.this, respuesta, Toast.LENGTH_LONG).show();
                    mostrarDatos(respuesta);
                    //textViewMuestra.setText(respuesta);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("dni", sDni);
            return params;
        }

    };

    RequestQueue colaSolicitudes = Volley.newRequestQueue(this);
    colaSolicitudes.add(sRequest);}
    
asked by JAMDZ 01.06.2016 в 07:10
source

2 answers

0

Well, in your Singleton as parameter you send the Context but when you make a call you have to be clear about where you are. If you are in an activity you use the this but if you are within a fragment you get the context with getACtivity()

    
answered by 02.06.2016 в 00:20
0

It seems to me that the problem is how you are defining your StringRequest, the method getParams() is out of place, it should be this way

StringRequest sRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String respuesta) {
                    //Toast.makeText(MainActivity.this, respuesta, Toast.LENGTH_LONG).show();
                    mostrarDatos(respuesta);
                    //textViewMuestra.setText(respuesta);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                 }
});


/*            }){
        @Override
        protected Map<String, String> getParams(){
            Map<String, String> params = new HashMap<String, String>();
            params.put("dni", sDni);
            return params;
        } 
};*/

Here is the documentation with an example link

As for your RequestQueue is correct, you use this , referring to the Activity, if you are in an Activity,

 RequestQueue requestQueue = Volley.newRequestQueue(this);

if you are in a Fragment use getActivity() as context.

 RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    
answered by 04.06.2016 в 20:14