Google Volley ignores POST parameter

I'm currently trying to send a simple POST request via Google Volley to my server. Therefore, I wrote the following lines of code:

Map<String, String> params = new HashMap<String, String>(); params.put("regId", "skdjasjdaljdlksajskl"); JSONObject object = new JSONObject(params); JsonObjectRequest request = new JsonObjectRequest(Method.POST, "address_of_my_server/method", object, successListener, errorListener); queue.add(request); 

But I get an error 500, which says that the parameter (regId) is missing. I tried the same with GET-Request but got the same result.

Only when I use StringRequest with a formatted URL, for example, "address_of_my_server / method? RegId = sadlasjdlasdklsj", the server responds to 200.

I get the same result when I use StringRequest, for example:

 StringRequest request = new StringRequest(Method.POST, "myurl", successListener, errorListener){ @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); params.put("regId", "skdjasjdaljdlksajskl"); return params; } }; 

Why is Wally ignoring my options?

+3
android android-volley
Nov 04 '13 at 1:06 on
source share
5 answers

EDIT:

I deleted my previous answer as it was not accurate.

I'll tell you what I know today: Apparently, getParams should work. But this is not always the case. I debugged it myself, and it seems that it is called when a PUT or POST request is executed, and the parameters provided in this method are in the regular GET parameters line ( ?param1=value1¶m2=value2 ...) and are encoded and placed in bodies.

I do not know why, but for some reason this does not work for some servers.

The best alternative way that I know for sending parameters is to put your parameters in a JSONObject and encode its contents in the request body using the request constructor.

+1
Nov 04 '13 at 8:20
source share

I had the same problem last week, but now it is fixed. Your server accepts the Content-Type as form data, when sending volley JsonObjectRequest the content type of the request will be application / json, so all parameters will be sent as a single json element, and not as a pair of key values, as in Stringrequest. Change the server code to get the request parameters from the body of the HTTP request, and not get it from the keys (for example, $ _REQUEST ['name'] in php).

+3
Nov 07 '13 at 3:16
source share

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<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); } } 
+3
Nov 13 '13 at 4:52
source share

Woking example with the issue Rajesh Butt mentioned

Java Code:

  JSONObject obj = new JSONObject(); try { obj.put("id", "1"); obj.put("name", "myname"); } catch (JSONException e) { e.printStackTrace(); } JsonObjectRequest jsObjRequest = new JsonObjectRequest( Request.Method.POST, url, obj, listener, errorlistener); RequestQueue queue = Volley.newRequestQueue(context); queue.add(jsObjRequest); 

PHP code:

  $body = file_get_contents('php://input'); $postvars = json_decode($body, true); $id = $postvars["id"]; $name = $postvars["name"]; 

Note:

PHP-Vars $_POST and $_REQUEST and $_GET are empty unless you send extra GET-VARS.

+2
Feb 13 '14 at 10:15
source share

Thi my decision

Solution 1

  public void getData() { final RequestQueue queue = Volley.newRequestQueue(this); StringRequest postRequest = new StringRequest(Request.Method.POST, "192.168.0.0/XYZ",new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONArray myArray = new JSONArray(response); for(int i = 0; i < myArray.length(); i++) { JSONObject jObj = myArray.getJSONObject(i); String category = jObj.getString("nameUser"); Log.e("value", category); } } catch (JSONException e) { e.printStackTrace(); Log.e("error: ", e.getMessage()); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //Toast.makeText(context,"Error : ").show(); } }){ @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("id_user", "1"); return params; } }; queue.add(postRequest); } 

Decision 2

remember that if you use php, $ _POST [''];
not working, her more info.

Luck

0
Sep 18 '15 at 5:11
source share



All Articles