简体   繁体   中英

Send an HttpPost request from Android, read Post from PHP

As I'm progressing through my Android learning in some spare time, I've encountered a strange behaviour of HttpPost request.

What I'm trying to achieve: Make a simple POST request from Android application to Apache web-server running on my development PC and display the POSTed data from PHP script to which the form is sent.

My Android app's Java code resides inside an Activity as an AsyncTask as following:

private class DoSampleHttpPostRequest extends AsyncTask<Void, Void, CharSequence> {

    @Override
    protected CharSequence doInBackground(Void... params) {
        BufferedReader in = null;
        String baseUrl = "http://10.0.2.2:8080/android";

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(baseUrl);

            List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("login", "someuser"));
            postParameters.add(new BasicNameValuePair("data", "somedata"));
            UrlEncodedFormEntity form = new UrlEncodedFormEntity(postParameters);
            request.setEntity(form);

            Log.v("log", "making POST request to: " + baseUrl);

            HttpResponse response = httpClient.execute(request);

            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();

            return sb.toString();
        } catch (Exception e) {
            return "Exception happened: " + e.getMessage();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onPostExecute(CharSequence result) {
        // this refers to a TextView defined as a private field in the parent Activity
        textView.setText(result);
    }

}

My PHP code is the following:

<?php
echo "Hello<br />";
var_dump($_SERVER);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "Page was posted:<br />";
    foreach($_POST as $key=>$var) {
        echo "[$key] => $var<br />";
    }
}
?>

And finally the problem : As you can see, the $_SERVER contents is dumped, and in the output $_SERVER["REQUEST_METHOD"] has value GET despite the fact that I was actually making a POST request. Even if I try to dump the contents of $_POST , it's empty.

What am I doing wrong? Thanks in advance.

You might need to specify a trailing slash at the end of the URL.

Often Apache redirects requests that don't end in trailing slashes so that they do contain a trailing slash. That redirect is a GET redirect (without some tweaking), so all POST data is lost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM