[英]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.