$ _POST数组在android中是空的

时间:2016-07-18 01:01:37

标签: php android

PHP没有收到从android发送的数据。从外观上看,Android应用程序似乎连接到服务器就好了。但$ _POST数组似乎是空的:

机器人:

        URL url;
        HttpURLConnection urlConnection;

        try
        {
            url = new URL("http://10.0.2.2:80/Web%20/Practice/Android%20Login/phpserver.php");

        }catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception5";

        }

        try
        {

                urlConnection = (HttpURLConnection) url.openConnection();

           // urlConnection.setReadTimeout(15000);
           // urlConnection.setConnectTimeout(10000);
            Log.d(TAG, "URL Connection enabled");

            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);

            Log.d(TAG, "Params [0]: " + params[0] +  "   Params[1]: " + params[1]);
            // Append parameters to URL
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("Username", params[0])
                    .appendQueryParameter("PhoneNumber", params[1]);


            String query = builder.build().getEncodedQuery();
            Log.d(TAG, "Query: " + query);
            Log.d(TAG, "Append Parameters to URL");

            // Open Connection for sending data
            OutputStream os = null;

            try {
                os = urlConnection.getOutputStream();
            }catch(Exception ex)
            {
                ex.printStackTrace();
                Log.e("ConcentratorExport", ex.getMessage() );
            }

            Log.d(TAG, "OutPutStream connection");
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

            Log.d(TAG, "BufferedWriter established");
            writer.write(query);

            Log.d(TAG, "Writing to Server");

            writer.flush();
            writer.close();
            os.close();

            urlConnection.connect();
            Log.d(TAG, "Connected");

        }catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception6";
        }


        try
        {

            int response_code = urlConnection.getResponseCode();

            if(response_code != 200)
            {
                Log.d(TAG, "Post failed");
                Log.d(TAG, "Post failed with " + response_code);
            }

            if(response_code == HttpURLConnection.HTTP_OK)
                return "Successful!";
            else
                return "Unsuccessful";

        }
        catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception7";
        }
        finally {
            urlConnection.disconnect();
        }

PHP:

      <?PHP

          $rawdata = file_get_contents("php://input");

          echo $rawdata;

          if(isset($_POST['Username']) && isset($_POST['PhoneNumber']))
           {
              $username  = $_POST['Username'];
              $phone_num = $_POST['PhoneNumber'];
              echo "Data sent";
              echo $username;
              echo $phone_num;
           } 
           else
              echo "No content was sent";


        ?>

从我的研究中,我用了

         file_get_contents("php://input")

获取任何HTTP正文请求。当我按下应用程序中的按钮并刷新页面时;没有打印到页面。发送数据在传输到服务器时丢失的信号?我不确定发生了什么,是什么导致了这一点。 Android应用程序正在模拟器上运行。

logcat的:

    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: URL Connection enabled
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Params [0]: Gina   Params[1]: 2225550000
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Query: Username=Gina&PhoneNumber=2225550000
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Append Parameters to URL
    07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: OutPutStream connection
    07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: BufferedWriter established
    07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Writing to Server
    07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Connected
    07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Successful!
    07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Printed on page

这是在Android应用程序中打包数据的方式:

        // Append parameters to URL
        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("Username", params[0])
                .appendQueryParameter("PhoneNumber", params[1]);

用户名和PhoneNumber是使用的密钥,我在php代码中使用相同的键作为索引。不完全确定问题是什么。有人可以说明导致这种情况的原因吗?

0 个答案:

没有答案