android 进度条 java_Java-Android:进度条,用于将数据上传到服务器

在Android应用中,开发者尝试通过创建一个进度条来显示数据上传到服务器的进度。他们使用了线程和ProgressDialog,但在实现过程中遇到一些错误,如`setVisibility`和`controller`未定义的问题。此外,还尝试使用AsyncTask进行文件上传,但同样遇到了错误。开发者需要解决这些问题以确保进度条能正确更新并成功上传数据。
摘要由CSDN通过智能技术生成

在我的应用程序中,有些数据包装在一个对象中.

我正在将此对象发送到服务器.一切正常.

在这里,我想显示数据加载到服务器时的进度条.

为此,我使用以下代码:

ProgressThread progThread;

ProgressDialog progDialog;

int typeBar;

int delay = 40;

int maxBarValue = 200;

@Override

protected Dialog onCreateDialog(int id) {

switch (id) {

case 1:

progDialog = new ProgressDialog(this);

progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

progDialog.setMax(maxBarValue);

progDialog.setMessage("Data uploading to the Server..");

progThread = new ProgressThread(handler);

progThread.start();

return progDialog;

default:

return null;

}

}

final Handler handler = new Handler() {

public void handleMessage(Message msg) {

// Get the current value of the variable total from the message data

// and update the progress bar.

int total = msg.getData().getInt("total");

progDialog.setProgress(total);

if (total <= 0) {

dismissDialog(typeBar);

progThread.setState(ProgressThread.DONE);

}

}

};

private class ProgressThread extends Thread {

final static int DONE = 0;

final static int RUNNING = 1;

Handler mHandler;

int mState;

int total;

ProgressThread(Handler h) {

mHandler = h;

}

@Override

public void run() {

mState = RUNNING;

total = maxBarValue;

while (mState == RUNNING) {

connectServerClass.saveOnServer(Object);

Message msg = mHandler.obtainMessage();

Bundle b = new Bundle();

b.putInt("total", total);

msg.setData(b);

mHandler.sendMessage(msg);

total--; // Count down

}

}

public void setState(int state) {

mState = state;

}

}

当用户单击按钮时:

typeBar = 1;

showDialog(typeBar);

connectServerClass.saveOnServer(Object)

通过以上行,我正在将对象发送到服务器.实际上,我正在将数据发送到另一个类,即connectServerClass,并且该类将对象发送到服务器.

但是此代码无法正常工作.这段代码会长时间连接到服务器.

我使用以下代码:

private class Uploader extends AsyncTask

{

private List files;

private boolean canceled;

private int uploaded;

private Account account;

private ProgressDialog uploadSeekBar;

public Uploader(Account a, List files)

{

this.account = a;

this.files = files;

}

@Override

protected void onPreExecute()

{

uploadSeekBar.setMax(files.size());

uploadSeekBar.setProgress(0);

uploadSeekBar.setVisibility(View.VISIBLE); //Error: the method setVisibility is undefined

}

@Override

protected void onPostExecute(Integer result)

{

uploadSeekBar.setVisibility(View.INVISIBLE);

Toast.makeText(Upload.this, result + " files uploaded", // Error: Upload cannot be resolved to a type

Toast.LENGTH_LONG).show();

}

@Override

protected void onCancelled()

{

// XXX need a way to actually cancel the last upload

Toast.makeText(Upload.this, "canceling upload", Toast.LENGTH_LONG)

.show();

this.canceled = true;

uploadSeekBar.setVisibility(View.INVISIBLE);

}

@Override

protected Integer doInBackground(Void... voids)

{

uploaded = 0;

try

{

Iterator it = this.files.iterator();

while (!canceled && it.hasNext())

{

File file = it.next();

it.remove();

String msg = "";

try

{

if (debugMode) // what is this debugMode

{

//Put your uploading code here.

msg = ("fake uploading " + file);

Thread.sleep(3000);

} else

{

msg = ("uploading: " + file);

controller.uploadFile(file, this.account); //Error: controller cannot be resolved

}

uploaded++;

publishProgress(msg);

} catch (IOException e)

{

controller.te("error uploading file: " + file);

controller.te("error uploading file: " + e);

} catch (InterruptedException e)

{

}

}

} catch (Exception e)

{

publishProgress("error uploading: " + e);

}

return uploaded;

}

@Override

protected void onProgressUpdate(String... strings)

{

uploadSeekBar.setProgress(uploaded);

updateUploadMessage(files.size());

Toast.makeText(Upload.this, strings[0], Toast.LENGTH_LONG).show(); //Error: The method updateUploadMessage(int) is undefined for the type FirstActivity.Uploader

}

}

但是我遇到了一些错误,我在该行的右侧提到了这一点.请给我建议.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值