【发布时间】:2013-05-17 14:59:40
【问题描述】:
我是 Android 编程新手,但对 PHP 有一定的了解。
我正在尝试通过发布请求将数据发送到我的服务器。我有以下代码:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1980"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inps = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inps));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
inps.close();
result=sb.toString();
Log.v("data-received",result);
我在互联网上的许多地方都找到了代码。
在 PHP 方面,我正在写这个:
<?php
echo "something".$_REQUEST['year'];
?>
但我只得到“某物”(在日志值“数据接收”中)作为我的应用程序端的输出?
我做错了什么?我需要设置任何环境变量等吗?
【问题讨论】:
-
内容类型设置为什么?需要特定于 PHP 才能确认。
-
我也添加了内容类型..仍然无法正常工作...
-
确保您的内容类型设置为“application/x-www-form-urlencoded”并且您的内容长度也已设置。我不太了解 Java API,无法说明它是否为您设置。在您的服务器端脚本中,执行以下操作:“print_r( $_SERVER ); print_r( $_POST );”。这将转储标题并将数据发布到屏幕,以便您可以直观地检查结果。让我知道你发现了什么。
-
嘿伙计,你是为 DH4 做宝石的 Shubham 吗? ;)
标签: php android http-post httpclient