簡體   English   中英

如何在Android中使用Volley將多個JSON對象/數據發送到服務器

[英]how to send multiple json objects /data to server using volley in android

我正在對http請求使用volley ...將multip數據傳遞到服務器。代碼工作正常,但是只有一個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);                 



}

請幫助!!

創建帶有必需字段的類User:用戶名,電子郵件,密碼,然后使用用戶數據創建List userList使用Gson將List轉換為Json String。

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

然后,您可以將單個原始用戶數據放入參數中,如下所示:

params.put("raw_user_data", rawUser)

現在,在服務器中,您可以解碼Json Array並通過迭代獲取所有數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM