簡體   English   中英

無法在Android中的服務器mysqli / Php上發布數據

[英]Unable to POST data on Server mysqli/Php in android

我只是想在服務器上發布數據。 這沒有錯誤,但是沒有插入數據。 我通過直接訪問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);

            ?>

但是當我使用http://test.com?user=user1&age=11&userName=wer45&password=23ssds之類的 GET方法手動測試它時

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();

        }
    }

看一個示例HTTPURLConnection ,有一個httpURLConnection.connect();。 最后,我在您的代碼中看不到。

請注意,您的代碼具有httpURLConnection.disconnect();。 在異常的捕獲部分。

免責聲明:假設您的代碼完整並且我是對的

如果您實際使用了Wireshark,您將看不到模擬器發出的流量。 這就是為什么它是一個非常有用的工具。 當您的代碼沒有錯誤或異常,但是缺少某些內容時,它可以提供信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM