0

AndroidアプリケーションからPHPスクリプトにPOSTデータを送信しようとしています。PHPスクリプトはどのように見えるべきですか?これは私が試したものですが、機能しません。

Androidコード:

class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.alex26.0fees.net/script.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }
}

PHPスクリプト:

<?php
if(isset($_POST['id']))
    echo $_POST['id'];
if(isset($_POST['stringdata']))
    echo $_POST['stringdata'];
?>
4

1 に答える 1

2

POSTを介してPHPスクリプトに送信されるものはすべて、$_POST配列で終了します。スクリプトがそれをどのように処理するかは別の質問です。$ _POSTのコンテンツを「myfile.txt」という名前のファイルに書き込む最も簡単なテスト(各リクエストがファイルのコンテンツを上書きすることに注意してください):

<?php

    file_put_contents("myfile.txt", print_r( $_POST, true ));

?>

スクリプトでのエコーは無意味です。サーバーの応答を消費したり表示したりしていないので、どのように「機能」するのでしょうか。

于 2012-11-19T17:27:35.327 に答える