sama sama - 2 months ago 9
Android Question

Undefined index, android doesn't 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){
this.context=context;
}

@Override
protected String doInBackground(String... params){

String location_url ="http://192.168.1.90/server_connection.php";
try{

URL url1=new URL(location_url);
HttpURLConnection httpURLConnection =(HttpURLConnection)url1.openConnection();
httpURLConnection.setRequestMethod("POST");
// httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
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));

buffer_writer.write(PostData);

buffer_writer.flush();
buffer_writer.close();
stream_upload.close();

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;
}
bufferreader.close();
stream_upload.flush();
stream_dawnload.close();
httpURLConnection.disconnect();

return result;
}

}catch (Exception e){
e.printStackTrace();
}

return null;
}


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

backGround.execute(String.valueOf(send_json));


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.

Answer

After reading this question's solution I edited my code : first I eliminate backGround.execute(String.valueOf(send_json));from my service and i sent the vriables as parameters to asyncTask,like below:

backGround.execute(String.valueOf(mylocation.getLatitude()),String.valueOf(mylocation.getLongitude()));

then in asyncTask class I make my json object. this is my doInbackground's code:

 @Override
protected String doInBackground(String... params){


    String location_url ="http://192.168.1.90/connection.php";


try{
        URL url1=new URL(location_url);

        HttpURLConnection httpURLConnection =(HttpURLConnection)url1.openConnection();

        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");



        OutputStream stream_upload=httpURLConnection.getOutputStream();
        BufferedWriter buffer_writer=new BufferedWriter(new OutputStreamWriter(stream_upload,"UTF-8"));

    JSONObject obj = new JSONObject();

    try {
        obj.put("Latitude", params[0]);
        obj.put("Longitude", params[1]);



    }catch (JSONException e){
        e.printStackTrace();
    }

    httpURLConnection.connect();

    buffer_writer.write(obj.toString());
    buffer_writer.flush();
    buffer_writer.close();
    stream_upload.close();





        int status = httpURLConnection.getResponseCode();
        if(status ==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;
            }
            bufferreader.close();
            stream_upload.flush();
            stream_dawnload.close();
            httpURLConnection.disconnect();

            return result;
        }


    }catch (Exception e){
        e.printStackTrace();
    }


    return null;

}

and the most important note is that : if u want to send json object to server ,u should add this code :

$json = file_get_contents('php://input');

echo (file_get_contents('php://input'));//for being aware of what is in the object.
 $request = json_decode($json, true);

and for using json's values:

$location_lat=$request['Latitude'];
$location_long=$request['Longitude'];

And one thing else,I tryed $location_lat=$_POST['Latitude'] , and again this error that i asked ,is apeared: Undefined Index.So I resulted for using json objects this method is not correct. Hope be useful .