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?

6
  • 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"}] Commented Apr 6, 2016 at 6:17
  • What is the issue you are facing? Commented Apr 6, 2016 at 6:17
  • are you JsonRequest class? Commented Apr 6, 2016 at 6: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: codeshare.io/B8ai0 ,So i looking for a easy way to post json data Commented Apr 6, 2016 at 6:37
  • 2
    here is your answer stackoverflow.com/questions/23220695/… Commented Apr 6, 2016 at 6:54

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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"));
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.