Search code examples
androidhttpswifihttpclientdelay

HttpClient on Android using Wifi causes delay (on initial request)


I'm having this wierd problem with HttpClient on Android. If I try to connect to a https url using WiFi I'm having a delay before the acctual request is sent. If I send the request over 3G the delay does not exist.

It does however, only occur on Android 2.2 and 2.3, If i run it on for example 2.1-update1 it works fine on wifi as well.

However, when sending requests right after the initial request - it works properly on Wifi as well - but only for a while. Then it goes back to taking 10 seconds for one, and then properly again for a while...

When running on 2.3: 11285 ms

And on 1.6: 617 ms

The code I'm using to try and solve this problem is this HttpManager and:

public class Main extends Activity {

Button button;
TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            doRequest();
        }

    });
}

private void doRequest() {
    HttpGet httpget = new HttpGet("https://url_goes_here/");

    HttpResponse httpResponse = null;
    try {
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        long before = System.currentTimeMillis();
        httpResponse = HttpManager.execute(httpget);
        long after = System.currentTimeMillis();

        HttpEntity entity = httpResponse.getEntity();
        String response = convertStreamToString(entity.getContent());
        Log.i("Test", response);

        TextView text = (TextView) findViewById(R.id.text);
        text.setText((String) "Time: " + (after-before) + "\n" + response);

    } catch (Exception e) {
        e.printStackTrace();
        // Simplified code a bit
    }
}

protected static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

The line where the delay occurs is HttpManager.execute(httpget); which also is the line I calculate the ms on.

Has anyone ever been facing this problem? I find this delay very annoying, and so will my users. Emulators behave the same way as an Xperia Mini Pro running 2.1-update1 where it works properly over Wifi, and a HTC Desire running CyanogenMod 7 (2.3) where it doesn't work properly.


Solution

  • There is a problem with the ssl stack included with android. I struggled with this as well. Go to the apache home, and get the org.apache.http java package (I used v4.1). Include that in your application and use the HttpClient from it directly, instead of using the HttpClient built into Android, and your SSL Handshake delay problem will be resolved.