Undefined index, Android does not send data to php code


I wrote a program to send JSON from my android device to my server (XAMPP). I tested my PHP code using a form and it received the data correctly.

My app, on the other hand, sends no data to my server. Executing var_dump($_POST) returns array(0) on the server side.

Here's my android code:

public BackGround(Context context){

protected String doInBackground(String... params){

    String location_url ="";

        URL url1=new URL(location_url);
        HttpURLConnection httpURLConnection =(HttpURLConnection)url1.openConnection();
       // httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
        httpURLConnection.setRequestProperty("Accept", "application/json");

        OutputStream stream_upload=httpURLConnection.getOutputStream();
        BufferedWriter buffer_writer=new BufferedWriter(new OutputStreamWriter(stream_upload,"UTF-8"));
        String PostData= URLEncoder.encode(String.valueOf(params));



        int HttpResult = httpURLConnection.getResponseCode();
        if (HttpResult == HttpURLConnection.HTTP_OK) {
            InputStream stream_dawnload = httpURLConnection.getInputStream();

            BufferedReader bufferreader = new BufferedReader(new InputStreamReader(stream_dawnload, "iso-8859-1"));
            String result = "";
            String line;
            while ((line = bufferreader.readLine()) != null) {
                result += line;

            return result;

    }catch (Exception e){

    return null;

I send the data with this code where send_json is my JSON object:


I think the problem is caused by the JSON object not being correctly inserted into the POST request body.

I know there are questions with similar issues but none of the solutions helped me.

I appreciate any help.

There are a couple of points here that you should pay attention.

First, as you mentioned, it seems the problem is mainly because your JSON data is not being properly formatted into your request. You are passing the data to what seems to be an AsyncTask with a varargs parameter: doInBackground(String... params). This will transform the String object you passed when calling execute(send_json) into a String[] object. Calling String.valueOf(params) will not return anything resembling the data you initially passed to the AsyncTask but a text representation of a String array that contains your data.

Second, I'd strongly suggest you use a 3rd party library to handle the networking code for you. The code you have to write in order to properly manage an HTTP connection is neither trivial nor concise. There are much better alternatives out there. OkHttp is a great example and I'd highly encourage you to use it.

By the way, I'm not discouraging you from knowing how to create and manage HTTP connections in java/android. You should definitely try to understand what is going on when you send and receive data. I'm just saying that you don't need to handle all that complexity yourself since there are libraries that do a very good job doing exactly that.