php java android POST方法问题


php java android POST method issue

我有android应用程序,检查是否在DB已经存在的电话号码。但它总是返回"+"即使电话号码不存在,这是我的代码:

  URL url = new URL(URL_CHECK);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("cphone", "UTF-8") + "=" + URLEncoder.encode(params[0], "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "UTF-8"));
                String response = "";
                String line ="";
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                IS.close();
                info = response;
                return info; 

变量"info"总是等于"+"。这是我的PHP脚本:

    <?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "registers";
$con = new mysqli($servername, $username, $password, $dbname);
 $user_name = $_POST["cphone"];  
 $sql_query = "select id from users where phone like '$user_name';";  
 $result = mysqli_query($con,$sql_query);  
 if(mysqli_num_rows($result) >0 )  
 {  
 $row = mysqli_fetch_assoc($result);  
 $name =$row["id"];   
 echo "$name";
 }  
 else  
 {   
 echo "+";  
 }  
 ?>  

php脚本工作时,代替$_POST["cphone"]我写的例子"+37455001100"。

您POST值的方式可能是错误的。您可以参考https://stackoverflow.com/a/2938787/2435238上正确发送参数的答案。