我想在对象发送到服务器时放置进度条。但我不知道我需要在 doInBackground() 方法中编写什么代码以及 onProgressUpdate() 中的代码 .
这里客户端Socket程序用于向服务器发送Object。
代码(内部类):
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) {
}
}
所以,我需要在 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);
那么我如何获取这个参数。
最佳答案
您好,这是您问题的答案,请查找link
这是如何将文件上传到服务器的示例。
这是您可以添加进度条的示例。
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();
关于java - Android:将对象上传到服务器的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979739/