Take the 2-minute tour ×

I have an app that makes calls to a web service and displays the data in an Android UI. The web service is also used for a browser based version of this same app. The browser version, running in the Android browser, loads faster than my Android App version. In the browser pages load about 200ms, in my app about 1000ms. I'd think it should be the other way around, if designed correctly. The web service is the same for either, so here is my Android code:

        ByteArrayEntity entity = new ByteArrayEntity(OutBuf.toByteArray());
        HttpPost post = new HttpPost(myUrlHiddenForPrivacy);
        //post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpClient httpClient;
        if(CacheHttpClient == null)
            CacheHttpClient = new DefaultHttpClient(params);
httpClient = CacheHttpClient;
        HttpResponse response = httpClient.execute(post);
        HttpEntity re = response.getEntity();
        Input = new RemoteStream.Input(re.getContent());

A lot isn't shown, but hopefully you can understand the basics and see if I have any obvious issues. I've tried with and without the USE_EXPECT_CONTINUE line, since I read that can help sometimes. My web server doesn't require authentication, so I don't think I want that in my case. My web service supports keeping the connection open, so I added the static "CacheHttpClient" variable which just keeps that variable in memory (global static variable) so it gets reused.

I'm sending binary data, only about 200 bytes or so, and getting back 5k - 20k data. I can't use XML or otherwise because I can't update the server side just yet.

In the debugger running on the emulator the httpClient.execute(post) line is where most of the time is spent.. but the debugger is super slow in general, so I would prefer to profile on a real device. I don't have a device I can connect to my PC and debug with though, but I do have a device I use for testing, a few of them. Is there an easy way to add some profile spots to my code that I can view on the device? This code runs as an AsyncTask -- I thought about updating a TextView or the title bar with some profile info, but maybe there is something easier, like a profile/debug log I can write to on the device?

..or maybe someone can just tell me why it's slow from experience? :)

Edit: HttpClientCache is defined as so:

public class Client
    public static HttpClient CacheHttpClient = null;

That is a class inside a Client.java file, the same class that contains the code above. So I think that would be Application scope?

share|improve this question
Where do you define CacheHttpClient, in Activity scope or Application scope? –  yorkw Jan 18 '12 at 8:13
Err, sorry for the comment, I'm going to edit my post instead.. learning how this works still. I added the info to the end of my original post now. –  eselk Jan 18 '12 at 16:11
I read up on "scope", and now can say that it is Activity scope. In my testing I'm running the code several times in a row while the activity is open though, and I have confirmed from debugging that the cache object is being used all times but the first, and it isn't only the first time that is slow. –  eselk Jan 18 '12 at 20:00

Your Answer



Browse other questions tagged or ask your own question.