Search code examples
codenameone

What is the proper way to track an upload progress using RequestBuilder?


I am trying to track an upload progress. I can do this when downloading by simply adding a ProgressListener to the NetworkManager

But when uploading and doing the same, I only get 1 call back with the final upload size

I see in the code that there are some private methods that deal with the Network event type

ConnectionRequest seems to have a public method that sets the request type setWriteRequest

I am using the terse RequestBuilder which by default sets the value of setWriteRequest to true as long as there is a body (which there is)

So I really don't know why only 1 callback is being returned with the upload size. Am I missing something?

My final code looks something like this

ActionListener<NetworkEvent> progressListener = new ActionListener<NetworkEvent>(){
    @Override
    public void actionPerformed(NetworkEvent evt) {
        int getSentReceived = evt.getSentReceived();//this only gets total size, no partial upload progress received
    }
};
NetworkManager.getInstance().addProgressListener(progressListener);
RequestBuilder req = Rest.post(url).body(body).header("Content-Type", "application/json").fetchAsJsonMap(resp -> {
    NetworkManager.getInstance().removeProgressListener(progressListener);
});

Edit: I have double checked this behavior on Simulator, Android and iOS


Solution

  • Edit: I ended up implementing this in iOS and Android natively and publishing a CN1Lib for it that uses a simple case (upload of single text file and track progress)

    https://github.com/javieranton-zz/ProgressUploader