-1
Guys I’m trying to import a CSV file into android phone, it makes the import the file appears there on android device monitor, but when I open it, it is empty. codes:
Class importFtpActivity:
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();
}
}
}
class Ftpcontroler:
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 import 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();
}
effectDownload:
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:
inside CVS on ftp:
Android Device:
inside the CVS that was imported into the cell phone:
Does anyone know why?
thanks since you
--- EDIT ---
found something, while debugging I noticed what the status in Ftpcontroller in the download method, it is returning false, on this line:
status = mFTP.retrieveFile(arqOrigem, desFileStream);
and the mFTP
is null when debug arrives in return status