Php is not receiving data from httpurlconnection

122 Views Asked by At

I am sending data from my Android app to php but php is not getting any data. I have doubt that I am missing something in my php code. To connect php and Android, is only php URL is needed in android code or something else I have to do? I have stunk into this problem. Any help would be appreciated. My full php code is:

 <?php
         $user_email=$_POST['email'];
         echo "Email is $user_email";
   ?>

My Android code is:

URL server_url = new URL("http://www.myURL.com/Jobs/login.php");

      HttpURLConnection urlc = (HttpURLConnection) server_url.openConnection();
        //header stuff
        urlc.setRequestMethod("POST");
        urlc.setRequestProperty("Content-Language", "en-US");
        urlc.setRequestProperty("Accept-Encoding", "identity");
        //params
        String urlParameters = "email="+mEmail;
        //send post
        urlc.setDoOutput(true);
         DataOutputStream wr = new DataOutputStream(urlc.getOutputStream());
         wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();   

        //read result
        BufferedReader in = new BufferedReader(
            new InputStreamReader(urlc.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                 response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString()
       );
1

There are 1 best solutions below

5
On

Your PHP code doesn't have issue for Optimized code with email filter, if email address there.

Kindly replace your old code.

<?php
//Replace your Code by Ajmal Praveen
if(isset($_POST['email'])){
$user_email=$_POST['email'];
//sanitizing email by Ajmal Praveen
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){
        echo 'Enter a valid email address';
    }
else
{
    //If email is proper show it
    echo 'Email is '.$user_email.'';
}
}
?>

You need to check your Android code to see if the field email is properly configured or not.
I'm 100% sure there is no issue with the PHP Code.