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

Java-Android:进度条,用于将数据上传到服务器,第1张

概述在我的应用程序中,有些数据包装在一个对象中.我正在将此对象发送到服务器.一切正常.在这里,我想显示数据加载到服务器时的进度条.为此,我使用以下代码:ProgressThreadprogThread;ProgressDialogprogDialog;inttypeBar;intdelay=40;intmaxBarValue=200;@Override

在我的应用程序中,有些数据包装在一个对象中.
我正在将此对象发送到服务器.一切正常.
在这里,我想显示数据加载到服务器时的进度条.
为此,我使用以下代码:

Progressthread progThread;ProgressDialog progDialog;int typebar;int delay = 40; int maxbarValue = 200;@OverrIDeprotected 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<VoID, String, Integer>    {        private List<file> files;        private boolean canceled;        private int uploaded;        private Account account;        private ProgressDialog uploadSeekbar;        public Uploader(Account a, List<file> 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<file> 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         }    }

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

解决方法:

我强烈建议您使用AsyncTask.

下面的代码段将帮助您了解AsyncTask的外观.

package org.sample;import java.io.file;import java.io.IOException;import java.util.Iterator;import java.util.List;import androID.accounts.Account;import androID.os.AsyncTask;import androID.vIEw.VIEw;import androID.Widget.Toast;    private class Uploader extends AsyncTask<VoID, String, Integer>    {        private List<file> files;        private boolean canceled;        private int uploaded;        public Uploader(Account a, List<file> files)        {            this.account = a;            this.files = files;        }        @OverrIDe        protected voID onPreExecute()        {            uploadSeekbar.setMax(files.size());            uploadSeekbar.setProgress(0);            uploadSeekbar.setVisibility(VIEw.VISIBLE);        }        @OverrIDe        protected voID onPostExecute(Integer result)        {            uploadSeekbar.setVisibility(VIEw.INVISIBLE);            Toast.makeText(Upload.this, result + " files uploaded",                    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<file> it = this.files.iterator();                while (!canceled && it.hasNext())                {                    file file = it.next();                    it.remove();                    String msg = "";                    try                    {                        if (deBUGMode)                        {                                                //Put your uploading code here.                            msg = ("fake uploading " + file);                            Thread.sleep(3000);                        } else                        {                            msg = ("uploading: " + file);                            controller.uploadfile(file, this.account);                        }                        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();        }    }
总结

以上是内存溢出为你收集整理的Java-Android:进度条,用于将数据上传到服务器全部内容,希望文章能够帮你解决Java-Android:进度条,用于将数据上传到服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073850.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存