Volley JsonObjectRequest Post request not working

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

问题:

I am using android Volley for making a request. So I use this code. I don't understand one thing. I check in my server that params is always null. I consider that getParams() not working. What should I do to solve this issue.

 RequestQueue queue = MyVolley.getRequestQueue();         JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,null,                 new Response.Listener() {                     @Override                     public void onResponse(JSONObject response) {                         System.out.println(response);                         hideProgressDialog();                     }                 },                 new Response.ErrorListener() {                     @Override                     public void onErrorResponse(VolleyError error) {                       hideProgressDialog();                     }                 }) {             protected Map getParams() throws AuthFailureError {                 Map params = new HashMap();                 params.put("id","1");                 params.put("name", "myname");                 return params;             };         };         queue.add(jsObjRequest);,>,>,>

回答1:

try to use this helper class

import java.io.UnsupportedEncodingException; import java.util.Map;     import org.json.JSONException; import org.json.JSONObject;     import com.android.volley.NetworkResponse; import com.android.volley.ParseError; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.Response.Listener; import com.android.volley.toolbox.HttpHeaderParser;  public class CustomRequest extends Request {      private Listener listener;     private Map params;      public CustomRequest(String url, Map params,             Listener reponseListener, ErrorListener errorListener) {         super(Method.GET, url, errorListener);         this.listener = reponseListener;         this.params = params;     }      public CustomRequest(int method, String url, Map params,             Listener reponseListener, ErrorListener errorListener) {         super(method, url, errorListener);         this.listener = reponseListener;         this.params = params;     }      protected Map getParams()             throws com.android.volley.AuthFailureError {         return params;     };      @Override     protected Response
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!