-2

I am using 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 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 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 ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43
  • 1
    you will be submitting this data on click of any button right, When you are clicking on that button check if all the mandatory fields are filled up then make the api call , otherwise show error – Sachin Rajput May 18 '18 at 12:15
  • share your code if you want more help. – Sachin Rajput May 18 '18 at 12:15
  • Yes on button click I am calling Api, but if other fields are not filled how to construct the JSON object – Satyam Gondhale May 18 '18 at 12:32
  • Possible duplicate of [How to send a POST request with JSON body using Volley?](https://stackoverflow.com/questions/40079174/how-to-send-a-post-request-with-json-body-using-volley) – Ali May 18 '18 at 12:34

1 Answers1

0

Just check the content if the fields are null, by doing this you can check if the field is null or not , so you will know if the field is empty or not

to do it just use an if statment

if(user_email==null || user_phone==null) {

Log.e("emailorphonenull,"some fields are incomplete");

}

you can do the same thing with .length() and check if(user_phone.length() == 0 || user_email.length() == 0)

so, this will check if either one of the values is null , that means that one of them maybe are empty but not both of them

Remember to run this check before you send your query , because you will see errors in your onResponse(); otherwise

Edit: in your sendData() method, before doing the request check if the values are empty or not

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();
            }

             if(user_email==null || user_phone==null) {

Toast.makeText(yourclass.this, "Some mandatory fields are empty", Toast.LENGTH_SHORT).show();

} else{


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

        }

    }

  }

}

This should help you, play with that if statment to check the values, you can use .length() too to make sure the fields are not empty

As i understand you need at least one of them to succefully send your POST request, so this will check if either one of the two are null or empty

also, where you use the method sendData() you can check for the values before executing the method

have a good day

happy coding

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77