Android volley, how to pass post parameters dynamically

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

I'm new to google's Volley network library (and also to Android !), and I'm trying to pass POST arguments in a dynamic way !

For now I am overiding the : getParams() method : And returning the params in an hard coded manner.

@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 "hard coded" strings...

First I tried to put my Map of params as a member of my class, but class members are not avaible in the getParams() method.

Maybe I could use a singleton class to wich I could give the parameters I want to pass and get them back using its instance in the getParams() method ? But I don't think that it would be the right manner.

Below is the hole code of my Volley's 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); 

回答1:

In that case , you can create a Class extends StringRequest. Add an attr to store params and return it in 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); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!