Sending JSON body through POST request in OKhttp in Android
<p>I have setting up OkHttpClient and successfully sending the GET request to the server. And also I could able to sending the POST request to the server with empty body tag.</p>

<p>Now, I'm trying to send the following JSON Object to the server. </p>

<pre><code>{
"title": "Mr.",
"first_name":"Nifras",
"last_name": "",
"email": "nfil@gmail.com",
"contact_number": "75832366",
"billing_address": "",
"connected_via":"Application"
}
</code></pre>

<p>For this I have trying to adding the OkHttpClient library class <code>RequestBody</code> but I fail to sending the JSON object as body of the http POST request. The following way I have try to build the body and process the post request.</p>

<pre><code>OkHttpClient client = new OkHttpClient();

    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return ApplicationContants.JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
              // This is the place to add json I thought. But How could i do this
        }
    };

    Request request = new Request.Builder()
            .url(ApplicationContants.BASE_URL + ApplicationContants.CUSTOMER_URL)
            .post(body)
            .build();
</code></pre>

<p>What is the way I send the JSON object to the server via POST request.</p>

<p>Thanks in advance.</p>