【问题标题】:how to send multiple json objects /data to server using volley in android如何在android中使用volley将多个json对象/数据发送到服务器
【发布时间】:2017-06-23 22:32:44
【问题描述】:

我正在使用 volley 进行 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);                 



}

请帮忙!!

【问题讨论】:

  • 多个 JSONObjects 的用户单个数组
  • 为什么不用像this这样的RestFul服务!

标签: android json


【解决方案1】:

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

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

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

params.put("raw_user_data", rawUser)

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

【讨论】:

    猜你喜欢
    • 2015-04-05
    • 2014-11-06
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    相关资源
    最近更新 更多