If you just need the βafterβ metrics, you can simply provide your own subclass of the appropriate ClientConnectionManager implementation and get the corresponding HttpConnectionMetrics after the request is returned to the manager.
As a simple (and slightly sh * tty) example, a subclass of SingleClientConnManager , like this:
class MeasuringClientConnManager extends SingleClientConnManager { private long mReceivedBytes = -1; private long mSentBytes = -1; public MeasuringClientConnManager(HttpParams params, SchemeRegistry schreg) { super(params, schreg); } @Override public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) { HttpConnectionMetrics metrics = conn.getMetrics(); mReceivedBytes = metrics.getReceivedBytesCount(); mSentBytes = metrics.getSentBytesCount(); metrics.reset(); super.releaseConnection(conn, validDuration, timeUnit); } public long getReceivedBytes() { return mReceivedBytes; } public long getSentBytes() { return mSentBytes; } }
and just paste it into your HTTP client like this:
BasicHttpParams params = new BasicHttpParams(); SchemeRegistry schreg = new SchemeRegistry(); schreg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // Add SSL here if you need it MeasuringClientConnManager conman = new MeasuringClientConnManager(params, schreg); DefaultHttpClient client = new DefaultHttpClient(conman, params); client.execute(new HttpGet("http://www.google.com")); System.out.println(conman.getSentBytes()); System.out.println(conman.getReceivedBytes());
Edit : An alternative example that uses the input and output buffers of a logging session along with a journal with a corrupted conductor + adding a listener interface.
class MeasuringClientConnManager extends SingleClientConnManager { interface WireListener { void onUpdate(long sent, long recv); } private WireListener mListener; private long mReceivedBytes = -1; private long mSentBytes = -1; private Wire mWire = new Wire(null) { public boolean enabled() { return true; };
Usage is very similar, but now with wire updates when sending stuff:
BasicHttpParams params = new BasicHttpParams(); SchemeRegistry schreg = new SchemeRegistry(); schreg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // Add SSL here if you need it MeasuringClientConnManager conman = new MeasuringClientConnManager(params, schreg); conman.setWireListener(new MeasuringClientConnManager.WireListener() { @Override public void onUpdate(long sent, long recv) { System.out.println("WIRE sent: " + sent + ", recv: " + recv); } }); DefaultHttpClient client = new DefaultHttpClient(conman, params); client.execute(new HttpGet("http://www.thirdbase.se"));