Android app sends data to PHP script - Expectation failed
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.pr开发者_StackOverflow社区intStackTrace();
} 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);
?>
The string text is filled with error 417 - expectation failed.
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.
You never put in the server URL. How is android supposed to know what to do with this
HttpPost httppost = new HttpPost("myscript.php");
You should also format your code better.
Thanks for the quick answer. The problem is that I did put a right url in the HttpPost but I didn't want to show it as it's on the cloud. That's why I get a response from the script.
You should do something like this:
HttpPost httppost = new HttpPost("myscript.php");
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
i think FAlmarri is right. you should put your complete url of your script in
HttpPost httppost = new HttpPost("myscript.php");
you should use
HttpPost httppost = new HttpPost("http://abc.com/folder path of your script/myscript.php");
精彩评论