Php没有从httpurlconnection接收数据

时间:2017-09-08 17:32:44

标签: php android-studio-2.3

我将数据从我的Android应用程序发送到php,但php没有获取任何数据。我怀疑我在PHP代码中遗漏了什么。要连接php和Android,android代码中只需要php URL或者其他我需要做的事情吗?我已经陷入了这个问题。任何帮助,将不胜感激。 我的完整PHP代码是:

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

我的Android代码是:

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 个答案:

答案 0 :(得分:-1)

如果有电子邮件地址,那么您的PHP代码对于带有电子邮件过滤器的优化代码没有问题。

请更换旧代码。

<?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.'';
}
}
?>

您需要检查Android代码,看看是否正确配置了字段电子邮件 我100%确定PHP代码没有问题。