0

i am using volley for the http request... to pass multip data to the server .the code is working fine ,but only one json object is passing to the server.. this is what i want to pass to the server to store in server's database.

 "usernname":"username1","email":"email1","password":"password1"
"usernname":"username2","email":"email2","password":"password2"
"usernname":"username3","email":"email3","password":"password3"



is it right to loop through the Map<String, String>??

***protected Map<String, String> getParams() throws AuthFailureError {


            Map<String, String> params = new HashMap<>();
            for(int i=0;i<3;i++){
                params.put("username", username+i); //post variable in the php code.."username""email","password"
                params.put("email", email+i);
                params.put("password", password+i);
                //registerUser();

            }
            return params;***


**here is the full function that i am using..**

private void registerUser() {

   final String email = "user1";
   final String username = "user1";
  final String password = "user1";



    StringRequest stringRequest = new StringRequest(Request.Method.POST,Constants.URL_REGISTER,new Response.Listener<String>() {
        @Override
        public void onResponse(String response) { /


            try {

                JSONObject jsonObject = new JSONObject(response);

                Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                // Handle error
                public void onErrorResponse(VolleyError error) {
                   // progressDialog.hide();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {


        @Override

        protected Map<String, String> getParams() throws AuthFailureError {


            Map<String, String> params = new HashMap<>();
            for(int i=0;i<3;i++){
                params.put("username", username+i); //post variable in the php code.."username""email","password"
                params.put("email", email+i);
                params.put("password", password+i);
                //registerUser();

            }
            return params;

        }
    };    

    RequestHandler.getInstance(this).addToRequestQueue(stringRequest);                 



}

Kindly please help!!

egom
  • 21
  • 5

1 Answers1

0

Create class User with required fields : username, email, password Then create List userList with your user data Use Gson to convert List to Json String .

String rawUser  = new Gson().toJson(userList )

Then you can put single raw user data to params as

params.put("raw_user_data", rawUser)

Now in server you can decode Json Array and get all data through iteration.

Dhiraj Sharma
  • 4,364
  • 24
  • 25