上传文件传输到服务器时如何设置进度条

时间:2015-08-07 11:35:49

标签: android android-progressbar

如何在android中将文件上传到服务器时创建进度条。我在下面给出了这段代码请帮帮我.....转移我的数据时显示进度条....请解决我的问题..这是我上传的文件类...

    public void doFileUpload(File file) {


                FTPClient client = new FTPClient();

                try {  
                    client.connect(FTP_HOST,21);
                    Log.e("clientconnect", "" + client);
                    client.login(FTP_USER, FTP_PASS);
                    Log.e("clientlogin", "" + client);
                    client.setType(FTPClient.TYPE_BINARY);
                    Log.e("clienttype", "" + client);
                    client.changeDirectory("/gg1/");
                    Log.i("", "$$$$$$$$$$$$$$$$$" + ("/gg1/"));


                    client.upload(file,new MyTransferListener());
                    Log.e("filenameupload", "" + file);



                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        client.disconnect(true);
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }

            }

        public class MyTransferListener implements FTPDataTransferListener {

            public void started() {

                Toast.makeText(getBaseContext(), " Upload Started ...",
                        Toast.LENGTH_SHORT).show();

            }

            public void transferred(int length) {


                Toast.makeText(getBaseContext(), " transferred ..." + length,
                        Toast.LENGTH_SHORT).show();

            }

            public void completed() {


                Toast.makeText(getBaseContext(), " completed ...",
                        Toast.LENGTH_SHORT).show();


            }

            public void aborted() {


                Toast.makeText(getBaseContext(),
                        " transfer aborted , please try again...",
                        Toast.LENGTH_SHORT).show();

            }

            public void failed() {


                System.out.println(" failed ...");
            }

        }

1 个答案:

答案 0 :(得分:1)

您基本上必须扩展<h1 ng-bind-html="data.text"></h1> 以进行使用互联网的活动

您的上传课程通常就像

class Parent():
        def __init__(self):
            print "Parent initialized"

        @classmethod    
        def resource_setup(cls):
            cls.name = "P"
            print "parent method"

        def parent_method(self):
            print self.name

您在主线程

中调用该类的实例
asynctask

布局文件类似于

 private class UploadFileToServer extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        // setting progress bar to zero
        progressBar.setProgress(0);
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {

        progressBar.setVisibility(View.VISIBLE);
        progressBar.setProgress(progress[0]);
        txtPercentage.setText(String.valueOf(progress[0]) + "%");

        btnUpload.setVisibility(View.GONE);
    }

    @Override
    protected String doInBackground(String... params) {
        return uploadFile(params[0]);
    }

    private String uploadFile(String name) {

        try {
        // FTP Upload code

    }

        } catch (IOException e) {

    }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

HTH。