Androi Developer
Androi Developer

Reputation: 297

Android Fileupload using FTP Protocal Not showing ProgressBar

In my android project i need to display seekbar and network speed(With how much speed the file is Uploading/Downloading from server using FTP Protocol). In my situation when i try to upload a file from emulator sdcard then it's showing the speed and seekbar at the end when the file is stored in the server, but my requirement is i need to show sheekbar and network speed continuously while the file is uploading into server. I am not understand where i did wrong in my code. Below is my code for file upload into ftp server:

Thanks In Advance.

    @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("[email protected]", "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;
    }    

Upvotes: 0

Views: 453

Answers (1)

Joe Malin
Joe Malin

Reputation: 8641

Since you're calling doInBackground(), I assume you're subclassing AsyncTask. If so, why aren't you using publishProgress and onProgressUpdate? runOnUiThread doesn't execute immediately unless you're already on the UI Thread, and because you're in doInBackground() you're in a background thread, not the UI Thread. There's a possibility that runOnUiThread() isn't actually running until doInBackground() is over. In comparison, publishProgress and onProgressUpdate are specifically designed to report progress from the background thread.

Upvotes: 1

Related Questions