【发布时间】:2014-01-02 08:29:05
【问题描述】:
我的 Android 应用程序中有一个函数可以将数据发送到 PHP 文件,该文件将其插入数据库。我的问题是 post 值实际上从未发送到 PHP 文件。
我的函数如下所示:
public void postData(){
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(myURL);
try {
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("firstName", "John"));
param.add(new BasicNameValuePair("lastName", "Doe"));
post.setEntity(new UrlEncodedFormEntity(param));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// show response in logcat
String line = "";
while ((line = rd.readLine()) != null) {
Log.i("postData", line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我的 php 文件只是检查变量是否已设置:
<?php
if (isset($_POST["firstName"]) && isset($_POST["lastName"])) {
// insert into the database
} else {
// send error message.
}
?>
我总是从 PHP 文件的 else 部分得到错误消息。有谁知道为什么会发生这种情况或如何解决这个问题?
【问题讨论】:
-
你在 $_REQUEST 中收到了什么 - 你能记录下来吗?
标签: php android http post send