Android:进度条将对象上传到服务器

我想在将对象发送到服务器时放入进度条。 但我不知道我需要在doInBackground()方法中编写什么代码以及onProgressUpdate()中的代码。

这里Client Socket程序用于将Object发送到服务器。

代码(内部类):

class DownloadFileAsync extends AsyncTask { @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) { } } 

所以,这里我需要在doInBackground()onProgressUpdate()中编写

我有一个对象serverObject ,我需要将其发送到服务器。

目前我正在使用以下代码将对象发送到服务器而没有任何进度条:

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

我如何将此代码写入doInBackground()方法并跟踪发送的%数据。

如何获得对象的大小。 很多网站都提到了文件的大小。 但就我而言,这是一个对象。

还有一件事要执行,我需要写:

 new DownloadFileAsync().execute(params); 

所以我如何得到这个参数。

嗨,这里是你的问题的答案,请找到链接

这是如何将文件上传到服务器的示例。

这是您可以添加进度条的示例。

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