我必须将9.7MB的JSON
数据从PHP服务器传输到Android设备。数据使用gzencode()
压缩并在设备上解压缩,然后存储在SQLite
数据库中。整个下载过程大约需要55秒或更长时间,具体取决于网络速度。
如何缩短下载时间/优化流程?
我尝试将文本缓存到文件中,然后在每次请求时将其发送,但是速度没有提高。既不将文件拆分为大块。
即使下载速度为40-50MB / s,也无法改善。
<?php
if (isset($_POST['confirmare'])) {
header('Content-Type: application/json');
require "../conexiune.php";
$cat = $_POST['confirmare'];
$sql = "SELECT ID, Text, Răspuns1, Răspuns2, Răspuns3, Imagine, Categorie, ImagineBlob FROM Întrebări Where Categorie = '$cat'";
$result = $conn->query($sql);
ob_start('ob_gzhandler');
class întrebare
{
public $ID;
public $Text;
public $Răspuns1;
public $Răspuns2;
public $Răspuns3;
public $Imagine;
public $Categorie;
public $ImagineGen;
public function __construct($IDp, $Textp, $Răspuns1p, $Răspuns2p, $Răspuns3p, $Imaginep, $Categoriep, $ImagineGenp)
{
$this->ID = $IDp;
$this->Text = $Textp;
$this->Răspuns1 = $Răspuns1p;
$this->Răspuns2 = $Răspuns2p;
$this->Răspuns3 = $Răspuns3p;
$this->Imagine = $Imaginep;
$this->Categorie = $Categoriep;
$this->ImagineGen =base64_encode($ImagineGenp);
}
}
if ($result->num_rows > 0) {
$string = "[";
while ($row = $result->fetch_assoc()) {
$obiect = new întrebare($row["ID"], $row["Text"], $row["Răspuns1"], $row["Răspuns2"], $row["Răspuns3"], $row["Imagine"], $row['Categorie'], $row['ImagineBlob']);
$string .= json_encode($obiect) . ",";
}
$string = rtrim($string, ",");
$string .= "]";
$gzdata = gzencode($string, 9);
echo $gzdata;
} else {
echo "fără rezultate";
}
$conn->close();
}
答案 0 :(得分:1)
我正在回答这个问题,以防其他人偶然发现此问题。问题是,无论您压缩和优化多少过程,都需要传输很多字节,尤其是如果您使用base64对图像进行编码。
为解决此问题,我创建了一个新表,用于存储所有图像。当我将对象从服务器发送到Android / iOS设备时,我仅将ID发送到每个对象的图像。当设备需要显示图像时,它将使用图像ID向服务器创建一个异步请求。这样,一次仅下载一个图像。解决该问题并使下载时间微不足道。
希望这会有所帮助,在过去的一年中,我在这个问题上一直处于绊脚石。