Android room how to dynamically pass message parameters

I'm new to the google Volley network library (and also on Android!) And I'm trying to pass POST arguments dynamically!

I am currently overcoming the getParams () method: And returning the parameters in hard coded form.

@Override
protected Map<String, String> getParams()
{
       Map<String, String>  params = new HashMap<String, String>();
       params.put("login", "my_login");
       params.put("password", "my_password");
       return params;
}

I would like to pass variables instead of encoded string strings ...

At first I tried to put my parameter map as a member of my class, but class members cannot be found in the getParams () method.

Maybe I could use the singleton class so that I can pass the parameters I want to pass and return them using my instance in the getParams () method? But I do not think that would be right.

The following is the hole code of my Volley request:

RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();

String url = "https://theUrlToRequest";

StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response) {
                            JSONObject mainObject = null;
                            try {
                                Log.i("app", "Result = " + response);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.i("app", "Fail on Login" + error.toString());
                        }
                    }
            ) {
                @Override
                protected Map<String, String> getParams()
                {
                    Map<String, String>  params = new HashMap<String, String>();
                    params.put("login", "my_login");
                    params.put("password", "my_password");

                    return params;
                }
            };

queue.add(postRequest);
+4
1

, StringRequest. attr getParams();

MyStringRequest extends StringRequest{

    private Map params = new HashMap();
    public MyStringRequest (Map params,int mehotd,String url,Listener listenr,ErrorListener errorListenr){
    super(mehotd,url,listenr,errorListenr)

        this.params = params

    }
    @Override
    protected Map<String, String> getParams(){

        return params;

    }

}

RequestQueue queue = VolleySingleton.getInstance().getRequestQueue();

String url = "https://theUrlToRequest";
Map<String, String>  params = new HashMap<String, String>();
params.put("login", "my_login");
params.put("password", "my_password");
MyStringRequest postRequest = new MyStringRequest (params ,Request.Method.POST, url,
    new Response.Listener<String>(){
    },
    new Response.ErrorListener(){
    }
);
queue.add(postRequest);
+1

Source: https://habr.com/ru/post/1623944/


All Articles