Android: Progress Bar to upload Object into server

I want to put progress bar when object is sent to the server. But I don't know what the code I need to write in doInBackground() method and what the code in onProgressUpdate() .

Here Client Socket program is used to send Object to the Server.

Code (Inner Class):

class DownloadFileAsync extends AsyncTask<String, String, String> {


@Override
protected void onPreExecute() {
dialog = new ProgressDialog(this);
    dialog.setMessage("Uploading...");
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setProgress(0);
    dialog.show();
}

@Override
protected String doInBackground(String... aurl) {
}

@Override
protected void onProgressUpdate(String... progress) {

}

@Override
protected void onPostExecute(String unused) {

}
}

So, here what I need to write in doInBackground() and in onProgressUpdate()

I have one object serverObject which I need to send to the Server.

Currently I am using following code to send object to the server without any progress bar:

Socket s = new Socket("xxx.xx.xx.xxx", xxx);

//to send object to the server
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(serverObject);
oos.flush();
if (s != null) {s.close();}
if (oos != null) {oos.close();}

how I write this code into doInBackground() method and keep track up to what % data is send.

How to get the size of the object. In lot of site they mention size of the File. But in my case this is a object.

And one more thing to execute this I need to write :

new DownloadFileAsync().execute(params);

So how I get this params.

Answers


hi here is the answer of your question please find the link

this is the example of how you can upload the file to server.

here is the example you can add the progress bar.

ProgressDialog pd;
private final Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
         case 1:
            pd = ProgressDialog.show(this, "uploading...", "Please Wait..", true,false);
            pd.setCancelable(false);

          break;
         case 2:
         pd.dismiss();
         break;
        }

}
};
 new Thread(new Runnable() {                
            @Override
            public void run() {                     
                try {                   
                         handler.sendEmptyMessage(1);
                         executeMultipartPost(bitmap,filename.jpg);
                         handler.sendEmptyMessage(2);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   

            }
        }).start();

Need Your Help

Searching for an example of class-binding by a interface

java interface mvp

I am trying try to implement a MVP-Pattern example in Java