如何使用android中的volley将多个json对象/数据发送到服务器

时间:2017-06-23 12:52:11

标签: android json

我正在使用排序来获取http请求...将多个数据传递给服务器。代码工作正常,但只有一个json对象传递给服务器..     这就是我想传递给服务器存储在服务器数据库中的内容。

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



}

请帮助!!

1 个答案:

答案 0 :(得分:0)

使用必填字段创建类用户:用户名,电子邮件,密码 然后使用您的用户数据创建List userList 使用Gson将List转换为Json String。

i <- 1    for (i in 1:(nrow(df)-1)){
if(df[i+1,1] == df[i,1]){df[i+1,2] <- 2}
 if(df[i+2,1] == df[i,1]){df[i+2,2 <- 3}
  else df[i+2,2] <- 1}

然后您可以将单个原始用户数据放入params

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

现在在服务器中,您可以解码Json数组并通过迭代获取所有数据。