blank csv file

-1
I'm trying to import a CSV file into the android phone, it does the import the file appears on the android device monitor, but when I open it, it is empty. codes:

ImportFtpActivity class:

package realsysten.com.br.sigarestaurante;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.app.ProgressDialog;

import java.io.File;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPFile;

import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

/**
 * Created by Vitor on 14/06/2016.
 */
public class importaFtpActivity extends AppCompatActivity {

    Spinner spImport;
    ArrayList<String> arquivosFTP = new ArrayList<String>();
    ProgressDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.importa_ftp);

        spImport = (Spinner) findViewById(R.id.spImport);
        ImportItens();

        Button b2 = (Button) findViewById(R.id.btnImpInfos);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(importaFtpActivity.this, "FTP",
                        "Sincrozinzando dados...", false, true);
                dialog.setCancelable(false);

                ChamaImport();
            }
        });

        Button biv = (Button) findViewById(R.id.btnVoltar);
        biv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(importaFtpActivity.this, OpcoesActivity.class);
                startActivity(i);
                finish();
            }
        });
    }

    public void ImportItens() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                listarArquivosFTP();
            }
        }).start();
    }

    public void ChamaImport() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                efetuarDownload();
                dialog.dismiss();
            }
        }).start();
    }

    public void listarArquivosFTP() {
        FTPController ftp = new FTPController();
        ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);
        //ftp.mudarDiretorio("/Import");
        FTPFile[] arquivos = ftp.dir("/Import");
        if (arquivos != null) {
            int lenght = arquivos.length;
            for (int i = 0; i < lenght; i++) {
                FTPFile f = arquivos[i];
                if (f.isFile()) {
                    arquivosFTP.add(f.getName());

                }
            }
            final ArrayAdapter<String> arraAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, arquivosFTP);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    spImport.setAdapter(arraAdapter);
                }
            });
        }
    }

    public void efetuarDownload() {
        String lstrArq = "";
        try {
            FTPController ftp = new FTPController();
            lstrArq = "/Import/" + spImport.getSelectedItem().toString();
            File lArquivos = new File(Environment.getExternalStorageDirectory(), lstrArq);
            ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);

            ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
        } catch (Exception e) {
            e.getStackTrace();
        }
    }
}

FTPControler class:

package realsysten.com.br.sigarestaurante;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import android.os.Environment;
import android.util.Log;

/**
 * Created by Vitor on 14/06/2016.
 */
public class FTPController{

    FTPClient mFTP;
    private String TAG = "classeFTP";

    public FTPFile[] dir(String diretorio) {
        try {
            FTPFile[] ftpFiles = mFTP.listFiles(diretorio);
            return ftpFiles;
        } catch (Exception e) {
            Log.e(TAG, "Erro: não foi possivel listar os arquivos e pastas do diretorio " +
                    diretorio + " . " + e.getMessage());
        }
        return null;
    }

    public boolean mudarDiretorio(String diretorio) {
        try {
            mFTP.changeWorkingDirectory(diretorio);
        } catch (Exception e) {
            Log.e(TAG, "Erro: não foi possivel mudar o diretorio para " + diretorio);
        }

        return false;
    }

    public boolean desconecta() {
        try {
            mFTP.disconnect();
            mFTP = null;
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Erro: ao desconectar. " + e.getMessage());
        }
        return false;
    }

    public boolean conectar(String host, String usuario, String senha, int porta){
        try {
            mFTP = new FTPClient();
            mFTP.connect(host, porta);
            if (FTPReply.isPositiveCompletion(mFTP.getReplyCode())) {
                boolean status = mFTP.login(usuario, senha);

                mFTP.setFileType(FTP.BINARY_FILE_TYPE);
                mFTP.enterLocalPassiveMode();

                return status;
            }
        } catch (Exception e) {
            Log.e(TAG, "ERRO: não foi possivel conectar " + host);
        }
        return false;
    }

    public boolean download(String diretorioOrigem, String arqOrigem, String arqDestino) {
        boolean status = false;

        try {
            File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/Import");

            if (!caminho.exists()) {
                caminho.mkdir();
            }

            mudarDiretorio(diretorioOrigem);

            File csvdestino = new File(arqDestino);
            FileOutputStream desFileStream = new FileOutputStream(csvdestino);
            mFTP.setFileType(FTP.BINARY_FILE_TYPE);
            mFTP.enterLocalActiveMode();

            status = mFTP.retrieveFile(arqOrigem, desFileStream);
            desFileStream.close();
            desconecta();
            return status;
        } catch (Exception e) {
            Log.e(TAG, "Erro: Falha ao efetuar download. " + e.getMessage());
        }
        return status;
    }

    public boolean upload(String diretorio, String nomeArquivo) {
        boolean status = false;
        try {
            FileInputStream arqEnviar = new FileInputStream(Environment.getExternalStorageDirectory() + diretorio);
            mFTP.setFileTransferMode(FTPClient.STREAM_TRANSFER_MODE);
            mFTP.setFileType(FTPClient.STREAM_TRANSFER_MODE);
            mFTP.storeFile(nomeArquivo, arqEnviar);
            desconecta();
            return status;
        } catch (Exception e) {
            Log.e(TAG, "Erro: falha ao efetuar upload. " + e.getMessage());
        }
        return status;
    }
}

The methods used for importing are:

Button 2:

Button b2 = (Button) findViewById(R.id.btnImpInfos);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = ProgressDialog.show(importaFtpActivity.this, "FTP",
                        "Sincrozinzando dados...", false, true);
                dialog.setCancelable(false);

                ChamaImport();
            }
        });

ChamaImport:

public void ChamaImport() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            efetuarDownload();
            dialog.dismiss();
        }
    }).start();
}

DownloadDownload:

public void efetuarDownload() {
        String lstrArq = "";
        try {
            FTPController ftp = new FTPController();
            lstrArq = "/Import/" + spImport.getSelectedItem().toString();
            File lArquivos = new File(Environment.getExternalStorageDirectory(), lstrArq);
            ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);

            ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
        } catch (Exception e) {
            e.getStackTrace();
        }
    }

download:

public boolean download(String diretorioOrigem, String arqOrigem, String arqDestino) {
        boolean status = false;

        try {
            File caminho = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/Import");

            if (!caminho.exists()) {
                caminho.mkdir();
            }

            mudarDiretorio(diretorioOrigem);

            File csvdestino = new File(arqDestino);
            FileOutputStream desFileStream = new FileOutputStream(csvdestino);
            mFTP.setFileType(FTP.BINARY_FILE_TYPE);
            mFTP.enterLocalActiveMode();

            status = mFTP.retrieveFile(arqOrigem, desFileStream);
            desFileStream.close();
            desconecta();
            return status;
        } catch (Exception e) {
            Log.e(TAG, "Erro: Falha ao efetuar download. " + e.getMessage());
        }
        return status;
    }

FTP:

insideCVSinftp:

AndroidDevice:

withintheCVSthatwasimportedintothephone:

Doesanyoneknowwhy?

thankyoufromja

---EDIT---

Ifoundonething,whendebugInoticedwhatstatusintheFTPControllerinthedownloadmethod,itisreturningfalse,inthisline:

status=mFTP.retrieveFile(arqOrigem,desFileStream);

and mFTP gets null when debug arrives at return status

    
asked by anonymous 15.06.2016 / 21:24

1 answer

0

I solved this problem, if someone has the same problem in the future, here is the form that I did and it worked out.

I just changed this line:

mFTP.enterLocalActiveMode();

for this:

mFTP.enterLocalPassiveMode();
    
16.06.2016 / 19:40