How to send json object to the webserver using post
<p>I want to send json object to my webserver. I make some changes in previous version of code, where i was sending strings to my webserver. but it is not working for sending object. Please help!  </p>

<pre><code>package com.digitalapplication.eventmanager;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class BackgroundTask extends AsyncTask&lt;JSONObject,Void,String&gt; {
    Context ctx;
    BackgroundTask(Context ctx)
    {
        this.ctx=ctx;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(JSONObject... params) {
        String inserturl="http://192.168.10.4/webapp/register.php";
        String method="register";
        if(method.equals("register"))
        {

            try {
                URL url=new URL(inserturl);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream OS=httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));

                bufferedWriter.write(params.toString());

                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS=httpURLConnection.getInputStream();
                IS.close();
                return "Data Saved in server...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return "not saved in server";
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String  result) {
        Toast.makeText(ctx, result,Toast.LENGTH_SHORT).show();
    }


}


        return "not saved in server";
    }
</code></pre>

<p>here is call to the backgroundTask class</p>

<pre><code>BackgroundTask backgroundTask=new BackgroundTask(this);

                JSONObject jsonObject=new JSONObject();
                try {
                    jsonObject.put("gid","asd");
                    jsonObject.put("uid","asdd");
                    jsonObject.put("name","assgd");
                    jsonObject.put("phone","agssd");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                backgroundTask.execute(jsonObject);
</code></pre>

<p>here is server side php script.</p>

<p>init.php</p>

<pre><code>&lt;?php
$db_name="eventmanager";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";
$con=mysqli_connect($server_name, $mysql_user,$mysql_pass,$db_name);
if(!$con){
    //echo"Connection Error...".mysqli_connect_error();
}
else{
    //echo"&lt;h3&gt;Connection success....&lt;/h3&gt;";
}

?&gt;
</code></pre>

<p>And</p>

<p>register.php</p>

<pre><code> &lt;?php
    require "init.php";
    $obj = $_POST["obj"];
    $args = json_decode($obj, true);
    foreach($args as $key=&gt;$field){
        $gid = $field["gid"];
        $uid = $field["uid"];
        $name = $field["name"];
        $phone = $field["phone"];
        $sql_query="insert into groups values('$gid','$uid','$name','$phone');";
        mysqli_query($con,$sql_query);

    }

    ?&gt;
</code></pre>