使用FTP协议的Android Fileupload未显示ProgressBar

时间:2012-10-27 15:16:21

标签: android

在我的android项目中,我需要显示搜索栏和网络速度(使用FTP协议从服务器上载/下载文件的速度)。在我尝试从模拟器SD卡上传文件的情况下,当文件存储在服务器中时它会显示速度和搜索栏,但我的要求是我需要在文件上传时连续显示sheekbar和网络速度进入服务器。我不明白我的代码在哪里做错了。下面是我上传到ftp服务器的文件代码:

提前致谢。

    @Override
    protected String doInBackground(String... arg0) {
        int count = 0;
        FTPClient ObjFtpCon = new FTPClient();
        //Toast.makeText(con, "FTPasync doInBackground() is called" ,Toast.LENGTH_SHORT).show();
        try {
            runOnUiThread(new Runnable() {
                public void run() {

                    bar.setProgress(0);
                    //real_time.setText(0 + " secs");
                    test_avg.setText(0+ " kbps");                       
                    //peak.setText(0+" kbps");
                }
            });
            updateUI(pp, R.drawable.pause);
            //ObjFtpCon.connect("ftp.customhdclips.com");
            ObjFtpCon.connect("ftp."+map.get("url").toString());
            updateUI(status, "Connecting");

            //if (ObjFtpCon.login("fstech@customhdclips.com", "fstech123")) {
            if (ObjFtpCon.login(map.get("username").toString(), map.get("password").toString())) {  
                updateUI(status, "Connected");
                ObjFtpCon.enterLocalPassiveMode(); // important!
                ObjFtpCon.cwd("/");// to send the FTP CWD command to the server, receive the reply, and return the reply code. 


                //if (mode == 0) {
                if(Integer.parseInt((map.get("oprn").toString()))== 0){  // Download File Using FTP Protocol
                    System.out.println("download test is called");
                    File objfile = new File(Environment.getExternalStorageDirectory() + File.separator + "/logo.png");

                    objfile.createNewFile();
                    FileOutputStream objFos = new FileOutputStream(objfile);
                    boolean blnresult = ObjFtpCon.retrieveFile("/logo.png", objFos);
                    objFos.close();
                    if (blnresult) {
                        // toast("Download succeeded");
                        // toast("Stored at : " +
                        // objfile.getAbsolutePath());
                    }

                    //***********************************************************

                    /*
                    File objfile = new File(
                            Environment.getExternalStorageDirectory()
                                    + File.separator + "/test.txt");

                    // System.out.println("total" + objfile.getTotalSpace() + " bytes");

                    objfile.createNewFile();
                    FileOutputStream objFos = new FileOutputStream(objfile);
                    boolean blnresult = ObjFtpCon.retrieveFile("/test.txt",
                            objFos);
                    objFos.close();
                    if (blnresult) {
                        System.out.println("download in ftp is successful");
                        // toast("Download succeeded");
                        // toast("Stored at : " +
                        // objfile.getAbsolutePath());
                    }*/
                } 

                else {
                    // Upload
                    System.out.println("upload test is called");
                    //Toast.makeText(con, "upload FTP test is called", Toast.LENGTH_SHORT).show();
                    //ContextWrapper context = null;
                    //assetManager= context.getAssets();

                    assetManager = getResources().getAssets();
                    input1 = assetManager.open("hello.txt");
                    long total = 0;
                    long sleepingTime= 0;

                    final long started = System.currentTimeMillis();
                    int size = input1.available();
                    byte[] buffer = new byte[size];
                    byte dataByte[] = new byte[1024];
                    //byte dataByte[] = new byte[(int)((CharSequence) input1).length()];
                    //input1.read(buffer);
                    //final int lenghtOfFile = data.getBytes().length;
                    final int lenghtOfFile = input1.toString().getBytes().length;
                    System.out.println("length of file....."+lenghtOfFile);
                    ByteArrayInputStream byteArrayIS = new ByteArrayInputStream(input1.toString().getBytes());
                    System.out.println("ByteArrayIS value is...."+byteArrayIS);
                    System.out.println("started time --"+started);
                    updateUI(status, "Uploading");
                    while ((count = byteArrayIS.read(dataByte)) != -1)
                    {   
                        System.out.println("read value is...."+byteArrayIS.read(dataByte));
                        while (sleep1) {
                            System.out.println("While Loop Sleep1 is Called");
                            Thread.sleep(1000);
                            sleepingTime +=1000;    
                        }

                        total += count;
                        System.out.println("Total Value IS:: "+total);
                        System.out.println("Count Value Is:: "+count);
                        final int progress = (int) ((total * 100) / lenghtOfFile);
                        System.out.println("Progress Value IS::" + progress);
                        final long speed = total;
                        //duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000; --- When using this line then it's not updating Avarage

                        boolean result = ObjFtpCon.storeFile("/test.txt", input1);
                        //boolean result = ObjFtpCon.storeFile(map.get("file_address").toString()+"/test.txt", input1); --- When using this line then it's not updating Status

                        duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
                        runOnUiThread(new Runnable() {
                            public void run() {     
                                    //bar.setProgress(progress);
                                    // trans.setText("" + progress);
                                    //duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
                                    //duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;  
                                    //real_time.setText(duration + " secs");
                                    if (duration != 0) {
                                        test_avg.setText((((speed / duration)*1000)*0.0078125)  + " kbps");
                                        bar.setProgress(progress);
                                        /*if (pk <= (speed / duration) / 1024) {
                                    pk = (speed / duration) / 1024;
                                }*/
                                        if (pk <= ((speed / duration)*1000)*0.0078125) {
                                            pk = (long)(((speed / duration)*1000)*0.0078125);
                                        }
                                        //peak.setText(pk + " kbps");
                                    }
                                }
                        });

                        //in.close();
                        if (result) {
                            updateUI(status, "Uploaded");
                            // toast("Uploading succeeded");
                            // toast("Uploaded at /test.txt");
                            //duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;
                            System.out.println("curreent time..... "+System.currentTimeMillis());
                            System.out.println("started time --"+started);
                            System.out.println("sleep tome...."+sleepingTime);
                            System.out.println("duration is....."+duration);
                            /*runOnUiThread(new Runnable() {
                            public void run() {
                                bar.setProgress(progress);
                                // trans.setText("" + progress);
                                //duration = ((System.currentTimeMillis() - started)-sleepingTime) / 1000;

                                real_time.setText(duration + " secs");
                                if (duration != 0) {
                                    test_avg.setText((speed / duration) / 1024
                                            + " kbps");
                                    if (pk <= (speed / duration) / 1024) {
                                        pk = (speed / duration) / 1024;
                                    }
                                    peak.setText(pk + " kbps");
                                }
                            }
                        });*/
                        }
                        /*while(!result){Thread.sleep(1000);}*/
                    }
                    byteArrayIS.close();
                }
            }

            else{
                System.out.println("password entered is incorrect");
                //Toast.makeText(con, "Username or/and password is incorrect", Toast.LENGTH_SHORT).show();
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
            // toast(e.getLocalizedMessage());
        }

        try {
            ObjFtpCon.logout();
            ObjFtpCon.disconnect();
        } 
        catch (IOException e) {
            e.printStackTrace();
            // toast(e.getLocalizedMessage());
        }
        return null;
    }    

1 个答案:

答案 0 :(得分:1)

因为你正在调用doInBackground(),所以我假设你是AslassTask的子类。如果是这样,为什么不使用publishProgress和onProgressUpdate? runOnUiThread不会立即执行,除非您已经 on UI线程,并且因为您在doInBackground()中,所以您在后台线程中,而不是UI线程。在doInBackground()结束之前,runOnUiThread()可能实际上没有运行。相比之下,publishProgress和onProgressUpdate专门用于报告后台线程的进度。