1

I am using Volley to transfer the data between Android device and web server.

I found a issue about sending a list of data to the server.

For example, my class will generate the data set like this:

{
  "1": {
    "1_aID": "5",
    "2_aID": "5",
    "3_aID": "5",
    "4_aID": "5"
  },
  "2": {
    "1_bID": "3",
    "2_bID": "3",
    "3_bID": "3"
  },
  "3": {
    "1_cID": "4"
  }
}

How can i send those data to Server?

I found some tutorial of Post data to server. It must using hashmap. Any better solution to handle this case?

user2520217
  • 319
  • 1
  • 4
  • 17
  • 1
    Well in my case I have converted the array to string and then send the data to server i have data like this is my application [{"color":"yellow"},{"color":"green"}] – Rakshit Nawani Apr 06 '16 at 06:17
  • What is the issue you are facing? – K Neeraj Lal Apr 06 '16 at 06:17
  • are you JsonRequest class? – Sumanth Jois Apr 06 '16 at 06:24
  • It is possible to use hashmap to store and send data. But, i need to write a function to separate those data. like this: https://codeshare.io/B8ai0 ,So i looking for a easy way to post json data – user2520217 Apr 06 '16 at 06:37
  • 2
    here is your answer http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley – Tashen Jazbi Apr 06 '16 at 06:54
  • another issue, if i put the request as a method (like that :https://codeshare.io/wlSBI), how can i receive the flag after success the request? like a return true or false. – user2520217 Apr 06 '16 at 09:40

2 Answers2

4
  1. Make a volley request like bellow which takes method like POST/GET, url, response & error listener. And For sending your json override getBody() method in which pass the json you want to send.
  2. Make a RequestQueue & add the request to it. You might start it by calling start()

Try this :

// Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://www.google.com";

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // your response

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // error
        }
    }){
        @Override
        public byte[] getBody() throws AuthFailureError {
            String your_string_json = ; // put your json
            return your_string_json.getBytes();
        }
    };
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
    requestQueue.start();

For more info see this

Sayem
  • 4,891
  • 3
  • 28
  • 43
  • If You are using php, don't forget that this JSON will be stored in `php://input` and not in `$_REQUEST` . Obtain it like `$obj = json_decode(file_get_contents("php://input"));` – Luka Govedič Aug 03 '17 at 18:13
1

Try this

JSONObject rootObj = new JSONObject();
JSONObject oneObject = new JSONObject();
oneObject.put("1_aID","5");
...
...
JSONObject threeObject = new JSONObject();
oneObject.put("1_cID","4");
rootObj("1",oneObject);
...
...
rootObj("3",threeObject);

 new JsonObjectRequest(Request.Method.POST,
                url,
                rootObj.toString(),
responselistner,
errorlistner);
crashOveride
  • 839
  • 6
  • 12