I am using volley in my application.I need to POST
one form to Server using Volley.The form contains some mandatory fields.If that fields are posted in JSON form, it is submitted successfully otherwise error is thrown.
This is json format I need to send
{
"email": "abc@gmail.com",
"address": "IUDP",
"phone": "9898981212",
"salutation": "Mr.",
"firstname": "PQR",
"lastname": "WXY",
"city": "Pune",
"country": "India",
"zip": "411006"
}
Email or Phone is mandatory fields.If only email or only phone is available, the data should POST
successfully and server not gives error.If other fields are included it should work fine.
This is my json post object.
final JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("salutation",user_saluation);
jsonObject.put("firstname",first_name);
jsonObject.put("lastname",last_name);
jsonObject.put("email",user_email);
jsonObject.put("phone",user_phone);
jsonObject.put("address",user_address);
jsonObject.put("city",user_city);
jsonObject.put("country",user_country);
jsonObject.put("zip","411001");
} catch (JSONException e) {
e.printStackTrace();
}
This is my request
private void sendData() {
String url= AppConstants.service_url+AppConstants.service_port+AppConstants.rest_service_path+"users/";
final JSONObject jsonObject = new JSONObject();
try {
// JSONObject loginback=new JSONObject();
jsonObject.put("salutation",user_saluation);
jsonObject.put("firstname",first_name);
jsonObject.put("lastname",last_name);
jsonObject.put("email",user_email);
jsonObject.put("phone",user_phone);
jsonObject.put("address",user_address);
jsonObject.put("city",user_city);
jsonObject.put("country",user_country);
jsonObject.put("zip","411001");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(request,req);
}
}
}
I have called this method on Button Click Listener
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendData();
}
So how to include only mandatory fields in JSON post request or mandatory fields with some other fields if available ?