Java - Android无法使用httpPost发送数据

时间:2012-04-02 12:32:40

标签: java php android post http-post

我在使用httpPost在Android中发送数据时出现问题。我找到了一些例子,我没有任何错误或异常但在php网站上$ _POST总是空/ null。

所以这是我的代码:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xxxxxxx.com/test.php");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("v", "123"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        String responseText = EntityUtils.toString(response.getEntity());
        Toast.makeText(this, responseText, 5000).show();
        System.out.println(responseText);

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

PHP:

<?php $t=$_POST['v']; print $t; ?>

并且它不打印123 ...

此外:

如您所见,有一个String responseText 。 在那个字符串中我可以看到我想要打印的“123”。

2 个答案:

答案 0 :(得分:3)

<?php $t=$_POST['v']; print $POST['v']; ?>

你在打印中有拼写错误(你错过了“_”)。

更改打印$POST['v'];以打印$_POST['v'];或简单print $t;

答案 1 :(得分:0)

试试这个:

$putdata = fopen("php://input", "r");
while ($data = fread($putdata, 1024))
  $inputdata .= $data;