简体   繁体   中英

How to load an internet inputstream faster?

I'm developing an Android app which communicates with a web service. The webservice gets me some geometry files and because of that those files can get really really large.

Because you can't wait 1-3 minutes until you got the response I need to lower the loading time.

So here is some Code:

long startTime = System.currentTimeMillis();

    InputStream is = httpResponse.getEntity().getContent();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[1000000];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, nRead);
    }

    long endTime = System.currentTimeMillis();

    Log.d("ARks", (endTime - startTime) / 1000 + " seconds");

The time it takes to do this is always between 50 and 75 seconds (and this with rather small files).

Is there a way to speed this thing up? Maybe some Android specific stuff to allow the app more bandwidth?

If you know that bandwidth is your problem, compress your stream: http://docs.oracle.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html

And if you don't need all the data before you can process it, reduce the size of your buffer. This will allow you to process some objects while others are coming across the wire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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