本文介绍了Android应用程序将数据发送到PHP脚本 - 期望失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试开发一个应用程序需要将数据发送到MySQL数据库。为了实现这一目标,我创建了一个httppost如下:
I try to develop an app which needs to send data to a MySql database.In order to achieve it, I create a httppost as follows
public void postData() {
try {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myscript.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", "Noon"));
nameValuePairs.add(new BasicNameValuePair("level", "0"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String text = EntityUtils.toString(response.getEntity());
Log.i("","response = "+text);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在我的服务器,我有以下脚本:
On my server, I have the following script:
<?php
$link = mysql_connect("localhost", "user", "password")
or die("Impossible de se connecter : " . mysql_error());
echo 'Connected';
$db_selected = mysql_select_db('db', $link);
if (!$db_selected) {
die ('Impossible de sélectionner la base de données : ' . mysql_error());
}
mysql_query("INSERT INTO players (name, level) VALUES ('".$_REQUEST['name']."', '".$_REQUEST['level']."')");
mysql_close($link);
?>
该字符串文本充满了错误417 - 期望的失败。
The string text is filled with error 417 - expectation failed.
我理解这个问题,但不知道如何解决它。我想AP preciate您在这个问题上的帮助,我有点卡住了。
I understand the problem but does not understand how to fix it.I would appreciate your help on this issue as I am a bit stuck.
推荐答案
您从来不把服务器的URL。如何Android的应该知道如何处理这种
You never put in the server URL. How is android supposed to know what to do with this
HttpPost httppost = new HttpPost("myscript.php");
您也应该格式化你的code更好。
You should also format your code better.
这篇关于Android应用程序将数据发送到PHP脚本 - 期望失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!