通过HTML从Android应用程序将数据发布到PHP服务器

时间:2015-10-05 09:11:02

标签: php android html

我正在尝试使用此HTML代码通过HTML在PHP服务器上发布数据。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="http://localhost:/" method="post">
<input type="hidden" value="Expence Description" id="ExpenceDescription" name="ExpenceDescription" />
<input type="hidden" value="DA, TA, Others" id="Descriptions" name="Descriptions" />
<input type="hidden" value="100, 101, 102" id="Amounts" name="Amounts" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

这是我的android代码:

 HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(URL);

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                            nameValuePairs.add(new BasicNameValuePair("ExpenceDescription", "'" + expenses_head + "'"));
                            nameValuePairs.add(new BasicNameValuePair("Descriptions", "'" + total_desc + "'"));
                            nameValuePairs.add(new BasicNameValuePair("Amounts", "'" + total_amount + "'"));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 HttpResponse response = httpclient.execute(httppost);
                            HttpEntity entity = response.getEntity();
                            //HttpResponse response = httpclient.execute(httppost);
                            StatusLine statusLine = response.getStatusLine();

问题是我在响应行上收到内部服务器错误500.

2 个答案:

答案 0 :(得分:0)

你需要在动作中指定php文件..例如,如果你监听post参数的文件被称为server.php,那么在表单动作中定义如下,假设两者都是;这个html页面和php文件驻留在同一个webserver目录中,否则指定你的php文件的URL

<form action="server.php" method="post">

<input type="hidden" value="Expence Description" id="ExpenceDescription" name="ExpenceDescription" />

<input type="hidden" value="DA, TA, Others" id="Descriptions" name="Descriptions" />

<input type="hidden" value="100, 101, 102" id="Amounts" name="Amounts" />

<input type="submit" value="Submit" />

</form>

答案 1 :(得分:0)

您需要一种与语言无关的数据交换格式,如JSON,才能将数据从Android传输到PHP,反之亦然。

在PHP中,您可以使用json_encodejson_decode在json中格式化数据,在Android中,您可以使用JSONObjectJSONArray管理Json

查看这些链接(12)以获得详细说明的示例。