I'm trying to sync my new messages from the client to the php server. I want to send every message in a chat application as a xml element, with text, time and other attributes. So I create an arraylist and then add it to my namevaluepairs to post. The android service looks like that :
String s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
";
for (Message message : messages) {
s = s + "<message id='" + message.getId()
+ "' text='" + message.getText()
+ "' time='" + message.getTime()
+ "' id_sender='" + message.getIdMe()
+ "' id_receiver='" + message.getIdYou() + "'/>
";
}
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair("xml_messages", s));
I'm keeping it very simple. The php server reads my xml string with :
$xml=simplexml_load_string($_POST['xml_messages']);
But.... It doesn't work. The php server than should read a message at a time and insert it into the database. This is the remaining code:
$xml=simplexml_load_string($_POST['xml_messages']);
mysql_connect("localhost","root","Password");
mysql_select_db("ci");
foreach($xml->children() as $message) {
mysql_query("insert into _message_user values('','".$message->text."','".$message->time."','".$message->id_sender."','".$message->id_receiver."');") or die (mysql_error());
}
It seems it never enters the foreach loop. I've tried to send other xml files and it seemed to work pretty fine.