简体   繁体   English

从服务器发送.zip到Android应用的最佳方法

[英]Best way to send .zips to an Android app from a server

I am designing an application that will retrieve .zip files from a server and extract them to the sdcard on reception. 我正在设计一个应用程序,它将从服务器检索.zip文件,并在接收时将其提取到sdcard

I think i can figure out the extracting, but I am not sure what the best way to send the .zips from the server is. 我想我可以弄清楚提取的内容,但是我不确定从服务器发送.zip的最佳方法是什么。 It only need to be one way for now but in the future I would like to add version control if that matters. 它只是现在的一种方式,但是将来,如果这很重要,我想添加版本控制。

I did some research and looked into JSON but I could not find any examples of that sending .zip files. 我做了一些研究,调查了JSON,但找不到发送.zip文件的任何示例。 A http connection seems like a comparable option and I know I could receive .zips but I don't think version control would be as easy to implement. http连接似乎是一个可比较的选择,我知道我可以接收.zips,但我认为版本控制不那么容易实现。 Any suggestions for either of these two methods or if you think another way would be better, I'm open to that too. 对于这两种方法的任何建议,或者如果您认为另一种方法会更好,我也很乐意。

I think you can use a standard HttpUrlConnection on the android site and just give it a URL that points to your zip File on the server. 我认为您可以在android网站上使用标准的HttpUrlConnection ,并为其指定一个指向服务器上zip文件的URL

Then you can just download it to a local folder on the device. 然后,您可以将其下载到设备上的本地文件夹中。 Here is an example for how I download Excel files (zip and excel files are both binary - so it should be the same). 这是我如何下载Excel文件的示例(zip和excel文件都是二进制文件-因此应该相同)。

When you want to do some sort of version control you are probably better off doing this on the server side (if possible) with whatever the server offers (eg php). 当您想进行某种版本控制时,最好使用服务器提供的任何功能(例如php)在服务器端进行此操作(如果可能)。 Then make a small php script that checks the version of the available zip files and send a response (maybe a json response). 然后制作一个小的php脚本,检查可用zip文件的版本并发送响应(可能是json响应)。

        URL url = new URL("yourURL");

        HttpURLConnection httpConnection = (HttpURLConnection) url
                .openConnection();
        httpConnection.setRequestMethod("GET");
        httpConnection.setDoOutput(true);
        httpConnection.connect();


        InputStream input = httpConnection.getInputStream();

        OutputStream output = new FileOutputStream(new File(activity
                .getFilesDir().getPath() + "/" + "yourFileName.zip"));

        byte buffer[] = new byte[1024];

        int len1 = 0;
        int bytesWritten = 0;
        while ((len1 = input.read(buffer)) > 0) {
            output.write(buffer, 0, len1);
            bytesWritten += len1;
        }

        output.flush();
        output.close();
        input.close();

Use a manifest file and get that first. 使用清单文件并首先获取它。 Examine the manifest file (a file that contains a list of other files) and see if your local storage contains those files. 检查清单文件(一个包含其他文件列表的文件),然后查看本地存储中是否包含这些文件。 If it does - everybody's happy. 如果可以的话-每个人都很高兴。 If it doesn't then get the files that your manifest lists onto your local storage. 如果没有,则将清单中列出的文件放到本地存储中。

Don't use the code as mentioned above directly. 不要直接使用上述代码。 Use some UI feedback to tell the user that you're downloading some files (and preferably ask them first in case they want to swap to wifi). 使用一些UI反馈来告诉用户您正在下载一些文件(最好是先询问他们,以防它们要交换到wifi)。 Getting the manifest first is ok, just don't get the missing files without asking them. 首先获取清单是可以的,只是不要不询问就获取丢失的文件。

I do this for my game "barcode beasties" which downloads approx 20Mb of audio assets on first run. 我这样做是针对我的游戏“条形码野兽”,该游戏在首次运行时会下载大约20Mb的音频资产。 I can also add further audio bits without having to push out a new release. 我还可以添加其他音频位,而不必推出新版本。 Android is also clever enough to unzip zip files, but again, make sure you are telling the user what's happening and give them the ability to cancel or back without killing your main activity thread. Android也足够聪明,可以解压缩zip文件,但是同样,请确保您要告诉用户正在发生的事情,并让他们能够取消或返回而不杀死您的主要活动线程。

暂无
暂无

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

相关问题 在android中将大量事件发送到服务器的最佳方式 - Best way to send lots of events to server in android 将数据从android应用程序传输到服务器上的数据库的最佳方法是什么? - what is the best way to transfer data from android app to a database on server? 从Android应用程序频繁轮询服务器的最佳方法 - Best Way to Frequently Poll a Server from an Android App 从android->服务器->另一个android实时发送数据的最佳方式 - best way to send data from android->server->another android in realtime 从数据库向Android发送数据的最佳方法 - Best way to send data from DB to Android 将手机位置从移动应用程序发送到服务器的最佳方式是什么? - what is the best way to send phone(s) location from a mobile app to the server? 使用Java从Android向Web服务器发送和接收数据(POST和GET)的最佳方式? - Best way to send and receive data(POST and GET) from Android to web server using Java? 从我的 android 应用程序将传感器数据发送到我的计算机的最佳方式是什么? - What is the best way to send sensor data to my computer from my android app? 通过TCP将图像路径URL从本地PC发送到Android应用的最佳方法? - Best way to send an image path url from local PC to an Android app over TCP? 如果服务器是apollo graphql server,从android应用程序上传文件的最佳方法是什么? - What is the best way to upload a file from android app if server is apollo graphql server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM