简体   繁体   English

如何在服务器端拆分JSON,然后在客户端加入并流式传输

[英]How to split JSON in server side then join and stream it in client side

Currently i have a big JSON file download link with the size of 1GB. 目前,我有一个大小为1GB的大型JSON文件下载链接。 What i can do is download it using android app then parse contents by json streaming through gson. 我能做的是使用android应用下载它,然后通过gson通过json流解析内容。

But i noticed a problem when users try to download such big file worth 1GB. 但是当用户尝试下载价值1GB的大文件时,我注意到了一个问题。

  1. What if the user's internet broke down in the middle of download? 如果用户的互联网在下载过程中中断了怎么办?
  2. What if the user's device suddenly ran out of space? 如果用户的设备突然用完空间怎么办?

Based on the above scenarios, the download should start again at the beginning. 根据上述情况,下载应从头开始。

To fix this, I thought of a possible solution which is breaking the JSON into parts. 为了解决这个问题,我想到了一个可能的解决方案,即将JSON分解为多个部分。 So the app can check if a part of is already downloaded and it just simple means the app is resuming it's download from the last downloaded part thus saving data and time. 因此,该应用程序可以检查是否已下载其中一部分,这很简单,意味着该应用程序将从上次下载的部分恢复其下载,从而节省了数据和时间。

My question is, how can i split the big json file? 我的问题是,如何分割大json文件? it's just a very big json file nothing special. 这只是一个很大的json文件,没什么特别的。 I'm using PHP to generate the JSON file from mysql with the codes: 我正在使用PHP从mysql用以下代码生成JSON文件:

$fp = fopen($target_dir.'/json.json', 'w');
 fwrite($fp,json_encode(array('reading'=>$r,'book'=>$b,'article'=>$a,'month'=>$m, 'status'=>$status)));
        fclose($fp);

Then producing the link via: 然后通过以下方式生成链接:

$file = $target_dir.'/json.json';

    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 540');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }

Furthermore, if i'm able to split the JSON file and create download links for the app to use, how would i join them via android app? 此外,如果我能够拆分JSON文件并创建供应用程序使用的下载链接,我将如何通过android应用程序加入它们? If joining is not necessary, can i parse them from chunks? 如果不需要加入,我可以从块中解析它们吗?

I don't know if it's the best approach for this kind of problem, but i noticed 3D mobile games like real racing and GTA san andreas download big files or maybe a bunch of medium sized files that take a while to finish. 我不知道这是否是解决此类问题的最佳方法,但我注意到诸如真实赛车和GTA san andreas之类的3D手机游戏会下载大文件或一堆中等大小的文件,这些文件可能需要一段时间才能完成。

One way to create chunk of files is using fread() PHP function. 创建文件块的一种方法是使用fread() PHP函数。 Using fread we can specify number of bytes we want to read from a file. 使用fread,我们可以指定要从文件读取的字节数。 So what you can do is create chunks of files after/while creating file from database. 因此,您可以做的是在数据库中创建文件之后/期间创建文件块。

This is how you can create chunks. 这就是创建块的方式。

$fp = fopen("jsonfile.txt",'r');
$i=1;
while(!feof($fp)) {
    $json_contents = fread($fp,4000);
    file_put_contents('json_chunk_'.$i.'.txt',$json_contents);
    $i++;
}

Now in android app keep a counter of which chunk was downloaded last and merge all the chunks at the end. 现在在android app中保留一个计数器,该计数器最后下载哪个块,并在最后合并所有块。

This might solve the problem if user's internet connection went down during downloading of data. 如果在数据下载过程中用户的互联网连接中断,这可能会解决问题。

That depends on data. 这取决于数据。 If data is like rows in DB with ID, client can download IDs one by one, so you won't need a chunk of data, just a row. 如果数据就像DB中具有ID的行,则客户端可以一个一个地下载ID,因此您只需要一行就不需要大量数据。 When client loses connection on one row they will retry that one until there's no more data. 当客户端在一行上失去连接时,他们将重试该行,直到没有更多数据为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM