简体   繁体   中英

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM