Volley JsonObjectRequest mail request not working

I am using android Volley to query. Therefore, I use this code. I do not understand anything. I check my server that the parameters are always zero. I believe getParams () does not work. What to do to solve this problem.

RequestQueue queue = MyVolley.getRequestQueue(); JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { hideProgressDialog(); } }) { protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("id","1"); params.put("name", "myname"); return params; }; }; queue.add(jsObjRequest); 
+74
android android-volley
Nov 07 '13 at 13:58
source share
8 answers

try using 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<JSONObject> { private Listener<JSONObject> listener; private Map<String, String> params; public CustomRequest(String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.listener = reponseListener; this.params = params; } public CustomRequest(int method, String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(method, url, errorListener); this.listener = reponseListener; this.params = params; } protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return params; }; @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } @Override protected void deliverResponse(JSONObject response) { // TODO Auto-generated method stub listener.onResponse(response); } } 

In activity / fragment use this

 RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener()); requestQueue.add(jsObjRequest); 
+126
Nov 13 '13 at 4:59
source share

You can create a custom JSONObjectReuqest and override the getParams method, or you can provide them in the constructor as JSONObject , which will be placed in the request body.

Like this (I edited your code):

 JSONObject obj = new JSONObject(); obj.put("id", "1"); obj.put("name", "myname"); RequestQueue queue = MyVolley.getRequestQueue(); JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,obj, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { System.out.println(response); hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { hideProgressDialog(); } }); queue.add(jsObjRequest); 
+27
Nov 07 '13 at 14:51
source share

Easy for me! I got this a few weeks ago:

This is used in the getBody() method, and not in getParams() for the mail request.

Here is my:

  @Override /** * Returns the raw POST or PUT body to be sent. * * @throws AuthFailureError in the event of auth failure */ public byte[] getBody() throws AuthFailureError { // Map<String, String> params = getParams(); Map<String, String> params = new HashMap<String, String>(); params.put("id","1"); params.put("name", "myname"); if (params != null && params.size() > 0) { return encodeParameters(params, getParamsEncoding()); } return null; } 

(I assumed that you want POST parameters that you wrote in getParams)

I gave params a request inside the constructor, but since you create the request on the fly, you can hardcode them inside your override of the getBody () method.

This is what my code looks like:

  Bundle param = new Bundle(); param.putString(HttpUtils.HTTP_CALL_TAG_KEY, tag); param.putString(HttpUtils.HTTP_CALL_PATH_KEY, url); param.putString(HttpUtils.HTTP_CALL_PARAM_KEY, params); switch (type) { case RequestType.POST: param.putInt(HttpUtils.HTTP_CALL_TYPE_KEY, RequestType.POST); SCMainActivity.mRequestQueue.add(new SCRequestPOST(Method.POST, url, this, tag, receiver, params)); 

and if you want even more, then the last parameters of the line come from:

 param = JsonUtils.XWWWUrlEncoder.encode(new JSONObject(paramasJObj)).toString(); 

and paramasJObj is something like this: {"id"="1","name"="myname"} regular JSON string.

+5
Nov 07 '13 at 14:08
source share

When you work with a JsonObject request, you need to pass parameters immediately after passing the link in the initialization, look at this code:

  HashMap<String, String> params = new HashMap<>(); params.put("user", "something" ); params.put("some_params", "something" ); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "request_URL", new JSONObject(params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // Some code } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //handle errors } }); } 
+2
Dec 22 '17 at 18:38
source share

All you have to do is override the getParams method in the Request class. I had the same problem and I was looking for answers, but I could not find a suitable one. The problem, unlike the query request, the sending parameters redirected by the servers can be deleted. For example, read this . Thus, do not risk your requests being redirected by the web server. If you are targeting http: // example / myapp , provide the exact address of your service, i.e. http://example.com/myapp/index.php .
Volleyball is fine and works fine, the problem occurs elsewhere.

+1
Jan 18 '16 at 19:41
source share

The override getParams function works fine. You use the POST method and you set jBody to null. That is why it does not work. You can use the GET method if you want to send null jBody. I override the getParams method, and it works with either the GET method (and null jBody) or the POST method (and jBody! = Null)

Also there are all examples here

+1
Nov 16 '16 at 11:49
source share

I had the same problem: an empty POST array is called due to request redirection (on the side of your server), correct the URL so that it does not redirect when it hits the server. For example, if https is forcibly using the .htaccess file in a server-side application, make sure your client request is prefixed with "https: //". Usually, when a redirect occurs, the POST array is lost. Hope this helps!

+1
May 17 '18 at 9:57
source share

This error occurred with its server part. I have an extra blank field that I generally used

0
Jul 10 '19 at 8:57
source share



All Articles