Take the 2-minute tour ×

I'm trying to get the progress of an Apache HTTP Client Execute method. Is it possible to either

  1. Get the mount of input stream that has been read by the execute method or
  2. Monitor the execute process using some internal method either by percent or amount of data sent?

The code below sends an input stream to the server (in this case an image) which is then stored appropriately. Problem is with high resolution cameras and slow mobile operators its hard to tell if an upload is actually taking place. The code does work, but feedback is desired.

public List writeBinary(String script, InputStream lInputStream) {
        Log.d(Global.TAG,"-->writing to server...");

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5*1000);
        HttpConnectionParams.setSoTimeout(httpParameters, 60*1000);

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx" + script);

        String responseText = null;
        List responseArray = new ArrayList();

        try {
            httppost.setEntity(new InputStreamEntity(lInputStream, -1));
            HttpResponse response = httpclient.execute(httppost);

            if (response.getStatusLine().getStatusCode() == 200){
                InputStream lInputStreamResponse = response.getEntity().getContent();
                DataInputStream lDataInputStream = new DataInputStream(lInputStreamResponse);
                BufferedReader lBufferReader = new BufferedReader(new InputStreamReader(lDataInputStream));

                while ((responseText = lBufferReader.readLine()) != null){


        } catch (ClientProtocolException e) {
        } catch (IOException e) {

        return responseArray;
share|improve this question

1 Answer 1

I prefer to "decorate" the stream/reader/writer object a little bit, add the desired progress feature into it. In decorated stream/reader/writer, you can keep a counter to indicate the progress, you can even put a indicator callback into it. ;-)

Something like this:

public Class CountableOutputStream implements OutputStream { 
    private int counter;
    private OutputStream origStream;

Actually, the whole Java IO package demonstrate this design pattern fairly well.

share|improve this answer

Your Answer


