我在 Android 中使用 httpPost 发送数据时遇到问题。我找到了一些示例,并且没有任何错误或异常,但在 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...
添加:
如您所见,有一个字符串responseText。 在该字符串中,我可以看到有我想要打印的“123”。
最佳答案
<?php $t=$_POST['v']; print $POST['v']; ?>
您的打印内容有拼写错误(缺少“_”)。
将 print $POST['v'];
更改为 print $_POST['v'];
或简单地 print $t;
关于Java - Android无法使用httpPost发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976506/