1

I'm trying to send a JsonObjectRequest to my server with some params, but seems like params doesn't arrive at the server. Before to post on SO I try all kind of suggestion found in google but no one works fine..

This is the code of my JsonObjectRequest:

RequestQueue queue = MySingleVolley.getInstance(ctx).
            getRequestQueue();

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Success: " + response.toString());
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error);
                }
            }){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("Content-Type", "application/json");
                        return headers;
                    }
                    @Override
                    protected Map<String, String> getParams() {
                        return params;
                    }
            };

    MySingleVolley.getInstance(ctx).addToRequestQueue(jsObjRequest);

And these are my param and others:

String url =  "url";

    //create the hashMap of parameters
    database_zappapp db = new database_zappapp(getApplicationContext());
    db.open();
    HashMap<String, String> params = new HashMap<>();
    params.put("action","myAction");
    params.put("nomeutente", db.getUsernameLogged());
    params.put("token", token);
    db.close();


    //Send the request to the server
    Global.RequestJsonToServer(getApplicationContext(), url, Request.Method.POST, params);

Thanks in advance for the help!

Edit 2

I've changed my params in this creating a string jsonBody:

JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("action","gcmUserRegister");
        jsonObject.put("nomeutente",db.getUsernameLogged());
        jsonObject.put("token",token);
    }catch(JSONException e){
        e.printStackTrace();
    }
    String requestBody = jsonObject.toString();
    db.close();

and my request like this with getBody():

JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Success: " + response.toString());
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error);
                }
            }){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<>();
                        headers.put("Content-Type", "application/json");
                        return headers;
                    }
                    @Override
                    public byte[] getBody() {
                        try {
                            return requestBody == null ? null : requestBody.getBytes("utf-8");
                        } catch (UnsupportedEncodingException uee) {
                            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                                    requestBody, "utf-8");
                            return null;
                        }
                    }
            };

But already didn't work! =(

Postman screen:

No user found means that it enter in the if statement and so it works.. with android i receive "result": "null"

The postman screen with app/json:

enter image description here

dvdciri
  • 441
  • 6
  • 19
  • If you use Google's official volley lib, in your edited request, `params` should be a `JSONObject` instead of `HashMap`. Hope this helps! – BNK Oct 02 '15 at 22:37
  • Take a look at my answers [here](http://stackoverflow.com/questions/32745400/sending-a-post-request-with-jsonarray-using-volley/32775920#32775920) and [here](http://stackoverflow.com/questions/32197615/volley-send-jsonobject-to-server-with-post-method/32216762#32216762) and [here](http://stackoverflow.com/questions/32197615/volley-send-jsonobject-to-server-with-post-method/32216762#32216762). Hope this helps! – BNK Oct 02 '15 at 22:48
  • In my edited REQUEST the params are already a JsonObject and not work yet.. I take a look to your answers! Thanks – dvdciri Oct 03 '15 at 12:41
  • Any error message received or not? Moreover, use Postman in Chrome test send requests to your server. – BNK Oct 03 '15 at 12:53
  • No error received, I only receive the json printed on my server but show me that doesn't work cause not enter in the if. I've already tried with postman on chrome and the php work perfectly! – dvdciri Oct 03 '15 at 14:10
  • Take a look at my edit 2,, please... – dvdciri Oct 03 '15 at 14:28
  • Post your postman screenshot and how did you init the params in your edited request. If your server url is available in Internet, post it so that I can check. – BNK Oct 03 '15 at 14:28
  • The "method"'s value is 1? – BNK Oct 03 '15 at 14:30
  • What do you mean with value 1? Now I post my postman screen – dvdciri Oct 03 '15 at 14:32
  • The first param in JsonObjectRequest, method, if POST it should be 1. If 0, it is a GET request. – BNK Oct 03 '15 at 14:35
  • Is Request.Method.POST.. – dvdciri Oct 03 '15 at 14:36
  • In Postman, the second key is "username", however in your android, it's "nomeutente". Why so? – BNK Oct 03 '15 at 14:39
  • It doesn't matter, I wrong to type in postman.. but as i write, the fact that the result is noUserFound means that it enter in the if statement on the server so the "action" params arrive.. instead in android no – dvdciri Oct 03 '15 at 14:44
  • Put jsonObject into the 3rd param of JsonObjectRequest and remove (comment) getBody(), the run the app, check if it works or not. – BNK Oct 03 '15 at 14:48
  • It doesn't work yet.. maybe there are some problem with the content-type header sent to the server and the content-type of the php page? could be?Because if i change the content type of postman in application/json it doesn't work... – dvdciri Oct 03 '15 at 14:53
  • Your postman version different from mine. The body tab with "form-data", is it JSON? – BNK Oct 03 '15 at 14:56
  • ummm, I'm trying to send params in raw like application/json and give me the same result as android.. I'll post you the screen – dvdciri Oct 03 '15 at 15:07
  • I see, perhaps the params should be sent in "form-data" instead of "json", I wonder if it is a multipart form-data or not – BNK Oct 03 '15 at 15:12
  • I have just tested your server url with form-data, it's NULL also, here the screenshot https://drive.google.com/file/d/0B2HGUM4c0Ywpak1qWVcyVFBFUDQ/view?usp=sharing – BNK Oct 03 '15 at 15:17

2 Answers2

2

I've found the solution!

The problem was in the server not in the client, I was getting the data using POST but from the client I was sending a json object so my new php is:

$data = json_decode(file_get_contents('php://input'), true);

//echo "Action: ".$action;
//Registrazione del token
if($data['action'] == "gcmUserRegister"){
......

Thanks al lot to BKS!!!

dvdciri
  • 441
  • 6
  • 19
  • Glad your issue solved :) here is another screenshot https://drive.google.com/file/d/0B2HGUM4c0YwpeE9YemdsZTA4Qk0/view?usp=sharing. Moreover, I am BNK, not BKS (lol) – BNK Oct 03 '15 at 15:21
1

Change this part of your code:

 `JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,null ...`

To this:

 `JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,yourparams..`

Reason: if you are using the default Volley constructors thats the way to send params to Server.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29