Nick Grant
Nick Grant

Reputation: 21

Android Slow File Download vs iOS & BlackBerry - DefaultHttpClient

I am using the following code to download files, and I am finding that the performance is really really slow compared with virtually the same code on iOS and BlackBerry.

As well as testing on various different SDK versions and OSX vs Windows, I have also tried the App on various devices - HTC Desire, Samsung Galaxy, Huwei Pulse, HTC Wildfire - all with terrible performance vs. iPhone and BlackBerry devices.

Check out this video I made to compare the speeds on the 3 emulators: Android vs. iOS and BlackBerry

Here is the Android code:

FileOutputStream fos = null;
InputStream input = null;

try {   
fos = new FileOutputStream("XXXX");

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("XXXX");
        HttpResponse response = httpclient.execute(httpget);
        input = response.getEntity().getContent();    

byte[] buffer = new byte[8192];
int readBytes;
while (((readBytes = input.read(buffer, 0, buffer.length)) != -1)
&& !thePackage.getPackageStatus().equals(
PackageStatus.STATUS_CANCEL_DOWNLOAD)) {
fos.write(buffer, 0, readBytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}

if (input != null) {
try {
input.close();
} catch (IOException e) {
}

    }
}

I've tried BufferedHttpEntity and other buffering/threading strategies, but this version of the code is the best performing out of any of the different options we've tried. I've run extensive code profiling, and can't see too much time being lost between the top level functions and the native code in Dalvik/Apache Harmony.

Any ideas would be fantastic, because the bad performance is making our application virtually unusable.

Thanks,

Nick

Upvotes: 2

Views: 753

Answers (3)

accuya
accuya

Reputation: 1433

There is a lot of options for the connection. you may try to set some of them yourself
or
try to use AndroidHttpClient

Upvotes: 0

byeo
byeo

Reputation: 656

I've rewritten this answer, after talking to Nick in person (in September 2011), he stated that on a real device there was no issue.

The issue appears to be performance differences between the emulator and real hardware. He also checked he wasn't using the throttling.

I've never tried downloading large files using the emulator so I can't comment, other than to say Nick is happy now ;-)

Upvotes: 0

Mark Bakker
Mark Bakker

Reputation: 1348

The android documentations states that the FileOutputStream is not buffered and should be wrapped in a BufferedOutputStream.

    OutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream("XXXX"));

        // write to out

    } finally {
        if (out != null) {
            out.close();
        }

    }

More info at FileOutputStream

Upvotes: 2

Related Questions