【发布时间】:2012-04-02 12:32:39
【问题描述】:
我在 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”。
【问题讨论】:
-
您是否添加了必要的权限...?
-
我这样做了,并且在 responsetext 中我找回了 php 站点的源代码,所以似乎我可以到达它
-
@user1162316 :但您在 nameValuePairs 中只添加了一项并为 (2) 创建;并尝试
$t=$_REQUEST['v']而不是$t=$_POST['v']
标签: java php android post http-post