Я использую PHP для запроса базы данных mySQL на моем сервере и форматирования ответа как XML, который я затем анализирую в приложении.
Вот пример PHP с моего сервера .... Он также отправляет ответ размера, чтобы приложение могло отслеживать процент завершения загрузки, а также ответ имени файла. Также, если база данных пуста, она отправит фиктивную запись.
<?php
header("Content-type: text/text");
//get the values from the url query string
$user = $_GET['user'];
$pass = $_GET['password'];
$db = $_GET['database'];
$dbhost = 'localhost';
//create connection to mysql or tell user something has gone wrong
$connection = mysql_connect( $dbhost, $user, $pass ) or die('Could not connect to MySQL');
//select database or tell user something has gone wrong
mysql_select_db( $db ) or die('Could not select database');
// Construct and run query.
$sql = 'SELECT * FROM itemInfo WHERE itemStatus = "active" ORDER BY itemSortKey ASC';
//echo $sql;
$result = mysql_query($sql) or die('<result>fail</result>');
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<bodyimage>\n";
while($record = mysql_fetch_assoc($result)) {
$xml_output .= "\t<storeitems>\n";
$xml_output .= "\t\t<itemSortKey>" . $record['itemSortKey'] . "</itemSortKey>\n";
$xml_output .= "\t\t<itemDesc>" . $record['itemDesc'] . "</itemDesc>\n";
$xml_output .= "\t\t<itemPrice>" . $record['itemPrice'] . "</itemPrice>\n";
$xml_output .= "\t\t<productID>" . $record['productID'] . "</productID>\n";
$xml_output .= "\t\t<field1>" . $record['field1'] . "</field1>\n";
$xml_output .= "\t\t<field2>" . $record['field2'] . "</field2>\n";
$xml_output .= "\t\t<field3>" . $record['field3'] . "</field3>\n";
$xml_output .= "\t\t<field4>" . $record['field4'] . "</field4>\n";
$xml_output .= "\t\t<dateCreated>" . $record['dateCreated'] . "</dateCreated>\n";
$xml_output .= "\t\t<dateModified>" . $record['dateModified'] . "</dateModified>\n";
// Escaping illegal characters
//$row['text'] = str_replace("&", "&", $row['text']);
//$row['text'] = str_replace("<", "<", $row['text']);
//$row['text'] = str_replace(">", ">", $row['text']);
//$row['text'] = str_replace("\"", """, $row['text']);
//$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
$xml_output .= "\t</storeitems>\n";
//add this record to the output
//$output = '|facilityname|' .$record['facilityname'] . '|/facilityname||customername|' . $record['customername'] . '|/customername||imageurl|' . $record['imageurl'] . '|/imageurl|';
//print $output;
}
$xml_output .= "</bodyimage>";
header("HTTP/1.0 200 OK");
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="storeitems.xml"');
header("Content-Length: ".strlen ( $xml_output ));
// redundent.....$xml_output .= "</bmrcontrol>";
//echo strlen( $xml_output );
//the next statement relates if there are NO lines in the table - it sends a message
if ( strlen( $xml_output ) == 61 ) {
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<bodyimage>\n";
$xml_output .= "\t<storeitems>\n";
$xml_output .= "\t\t<itemSortKey>aaaa</itemSortKey>\n";
$xml_output .= "\t\t<itemDesc>Store Items Missing</itemDesc>\n";
$xml_output .= "\t\t<itemPrice>0.0</itemPrice>\n";
$xml_output .= "\t\t<productID>blank</productID>\n";
$xml_output .= "\t\t<field1>.</field1>\n";
$xml_output .= "\t\t<field2>.</field2>\n";
$xml_output .= "\t\t<field3>.</field3>\n";
$xml_output .= "\t\t<field4>.</field4>\n";
$xml_output .= "\t\t<dateCreated>2012-02-13 09:03:49</dateCreated>\n";
$xml_output .= "\t\t<dateModified>2012-02-13 09:03:49</dateModified>\n";
// Escaping illegal characters
//$row['text'] = str_replace("&", "&", $row['text']);
//$row['text'] = str_replace("<", "<", $row['text']);
//$row['text'] = str_replace(">", ">", $row['text']);
//$row['text'] = str_replace("\"", """, $row['text']);
//$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n";
$xml_output .= "\t</storeitems>\n";
$xml_output .= "</bodyimage>";
}
echo $xml_output;
if(mysql_num_rows($result)) {
//mysql_close($connection);
}
else
die('<result>fail</result>');
//mysql_close($connection);
?>