开发者

progress bar for http request - Blackberry

开发者 https://www.devze.com 2023-01-10 06:40 出处:网络
in my project i am using popupscreen with GaugeField for http request.Currently we are just incrementing the value of gaugefield开发者_StackOverflow中文版 with fixed rate and after http response we ju

in my project i am using popupscreen with GaugeField for http request.Currently we are just incrementing the value of gaugefield开发者_StackOverflow中文版 with fixed rate and after http response we just remove the popupscreen. so some times http request is completed when gauge field is in 40% or 60%.

But i want to synchronize gaugefield value with http request/responses. it means that popupscreen will always remove at 100%.


I don't have the code in front of me, but I something similar in a project several years ago.

I wrote a subclass of InputStream that wrapped around the InputStream object I got back from openInputStream(), reimplementing all the read() methods so they would increment a counter with the number of bytes read. Whenever the counter reached a certain threshold, it would update a GaugeField object that was passed into the subclass's constructor.

So your subclass would look something like this:

public GaugedInputStream extends InputStream
{
    private InputStream _inputStream = null;
    private GaugeField _gaugeField = null;
    private int _counter = 0;
    private int _threshold = 0;

    public void GaugedInputStream(InputStream inputStream, GaugeField gaugeField)
    {
        _inputStream = inputStream;
        _gaugeField = gaugeField;

        ... other constructor stuff ...
    }

    public int read() 
    {
        int byte = _inputStream.read();
        increment(1);
        return byte;
    }

    public int read(byte[] b) 
    {
        int bytes = _inputStream.read(b);
        increment(bytes);
        return bytes;
    }

    public int read(byte[] b, int off, int len)  
    {
        int bytes = _inputStream.read(b, off, len);
        increment(bytes);
        return bytes;
    }

    ... override other InputStream methods here ...

    private void increment(int bytes)
    {
        _counter = _counter + bytes;
        _threshold = _threshold + bytes;
        updateGaugeIfNeeded();
    }

    private void updateGaugeIfNeeded()
    {
        if (_threshold > 100)
        {
            updateGauge();
            _threshold = 0;
        }
    }

    private void updateGauge()
    {
        ... code to update the gauge ...
    }
}

I'm leaving out a lot of the guts here, but I hope this sets you in the right direction.

0

精彩评论

暂无评论...
验证码 换一张
取 消