【问题标题】:Unable to POST data on Server mysqli/Php in android无法在 android 中的服务器 mysqli/Php 上发布数据
【发布时间】:2016-02-17 20:38:57
【问题描述】:

我只是想在服务器上发布数据。 这没有错误,但没有插入数据。 我通过直接访问php页面使用GET方法进行检查,它可以工作,但是当我运行应用程序时,它不起作用。

PHP Script:



<?php               
        $conn=mysqli_connect("localhost","my_user","my_password","my_db");

                $name=$_POST["name"];
                $age=$_POST["age"];
                $userName=$_POST["userName"];
                $password=$_POST["password"];

                $statement=mysqli_prepare($conn,"INSERT INTO User (name,age,UserName,password) VALUES (?,?,?,?)");
                mysqli_stmt_bind_param($statement,"siss",$name,$age,$userName,$password);
                mysqli_stmt_execute($statement);
                mysqli_stmt_close($statement);
                mysqli_close($conn);

            ?>

但是当我使用 GET 方法手动测试它时 http://test.com?user=user1&age=11&userName=wer45&password=23ssds

and change the php scripts as :

<?php   
    $conn=mysqli_connect("localhost","my_user","my_password","my_db");

    $name=$_GET["name"];
    $age=$_GET["age"];
    $userName=$_GET["userName"];
    $password=$_GET["password"];

    $statement=mysqli_prepare($conn,"INSERT INTO User (name,age,UserName,password) VALUES (?,?,?,?)");
    mysqli_stmt_bind_param($statement,"siss",$name,$age,$userName,$password);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);
    mysqli_close($conn);

?>

上面的 GET 工作在这里,任何人都可以检查下面的代码并帮助我在这里找出问题。我无法跟踪,因为没有抛出错误。

public class storeUserDataAsyncTask extends AsyncTask<Void,Void,Void>{
        User user;
        GetUserCallBack userCallBack;

        public storeUserDataAsyncTask(User user,GetUserCallBack userCallBack){
            this.user=user;
            this.userCallBack=userCallBack;
        }
        @Override
        protected Void doInBackground(Void... params) {
            HashMap<String,String> dataToSend=new HashMap<>();
            dataToSend.put("name", user.name);
            dataToSend.put("age",user.age+"");
            dataToSend.put("userName",user.userName);
            dataToSend.put("password",user.password);
            HttpURLConnection httpURLConnection=null;
            try {
                URL url = new URL("http://nishantapp11.esy.es/Register.php");
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(10000);
                httpURLConnection.setReadTimeout(10000);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                //httpURLConnection.setChunkedStreamingMode(0);

                int serverResponseCode=httpURLConnection.getResponseCode();
                if(serverResponseCode==HttpURLConnection.HTTP_OK){

                }else{
                    Log.e("TAG","not ok");
                }

                OutputStreamWriter outputStreamWriter=new OutputStreamWriter(httpURLConnection.getOutputStream());
                outputStreamWriter.write(getPostDataString(dataToSend));
                outputStreamWriter.flush();

               /* OutputStream outputStream=httpURLConnection.getOutputStream();


                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                bufferedWriter.write(getPostDataString(dataToSend));
                bufferedWriter.flush();
                bufferedWriter.close();*/

            }catch (Exception e){
                e.printStackTrace();
            }
            finally {
                httpURLConnection.disconnect();
            }
            return null;
         }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressDialog.dismiss();
            userCallBack.done(null);
            super.onPostExecute(aVoid);
        }
        private String getPostDataString(HashMap<String,String> params) throws UnsupportedEncodingException{

            StringBuilder result=new StringBuilder();
            boolean first =true;
            for(HashMap.Entry<String,String> entry:params.entrySet()) {
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));

            }
            //Log.e("POST URL", result.toString());
            return result.toString();

        }
    }

【问题讨论】:

  • 你有什么表格吗?如果是,则显示代码。
  • 欢迎堆栈溢出。请阅读常见问题解答、导览和帮助部分。在这种情况下,它有助于隔离问题。使用wireshark查看android代码中是否有什么东西。使用 postman 生成 HTTP 帖子,看看这是否有效。检查 php 代码是否使用调试器或日志条目运行。这将使您更具体。哦,然后检查它是否与 Android 上的 cookie 无关...
  • 我认为表单数据没有任何问题。因为当我打印 POST URL 时,它会按预期显示字符串,例如 name=name1&age=11&userName=test1&password=pass
  • @Roy Falk 我用 postman 测试过,数据请求和响应工作正常.. 你能告诉我编码部分是否有问题

标签: java php android mysqli


【解决方案1】:

查看sample HTTPURLConnection,末尾有一个 httpURLConnection.connect();,我在您的代码中没有看到。

请注意,您的代码在异常的捕获部分中有 httpURLConnection.disconnect();

免责声明:假设您的代码是完整的并且我是正确的

如果你真的使用过wireshark,你就不会看到你的模拟器没有流量。这就是为什么它是一个非常有用的工具。当您的代码没有错误或异常,但只是缺少某些内容时,它可以提供信息。

【讨论】:

  • 是的,这是错误之一,但之后我也遇到了其他错误。感谢您介绍 Wireshark,它对调试有很大帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 2016-02-11
相关资源
最近更新 更多