1
I am creating an app and I am trying to import some csv files that are on ftp into android.
then I have hers the following way:
Spinner will list all the files present in the "import" folder that is inside my FTP.
however when will perform this listing that occurs the error, we go to the codes:
Importaftpactivity 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 android.os.Bundle;
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.EditText;
import android.widget.Spinner;
import android.widget.Toast;
/**
* 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());
}
}
ArrayAdapter<String> arraAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, arquivosFTP);
spImport.setAdapter(arraAdapter);
}
}
public void efetuarDownload() {
String lstrArq = "";
try {
FTPController ftp = new FTPController();
lstrArq = "/" + spImport.getSelectedItem().toString();
File lArquivos = new File(Environment.getExternalStorageDirectory(), lstrArq);
if (!lArquivos.exists()) {
lArquivos.mkdir();
}
ftp.conectar("192.168.2.5", "vitor", "248693751qQ", 21);
ftp.download("/Import", spImport.getSelectedItem().toString(), lArquivos.toString());
} catch (Exception e) {
e.getStackTrace();
}
}
}
Class Ftpcontroller:
package realsysten.com.br.sigarestaurante;
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 {
mudarDiretorio(diretorioOrigem);
FileOutputStream desFileStream = new FileOutputStream(arqDestino);
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;
}
}
when the application runs the ImportItens();
that is in the way onCreate
class ImportFTP
, he does all the execution right, the ftp.conectar
, the ftp.mudarDiretorio
, the ftp.dir
, but when it comes to spImport.setAdapter(arraAdapter);
the following error occurs:
error:
06-15 12:19:47.433 31113-31623/realsysten.com.br.sigarestaurante E/Androidruntime: FATAL EXCEPTION: Thread-321 Process: realsysten.com.br.sigarestaurante, PID: 31113 android.view.Viewrootimpl$Calledfromwrongthreadexception: Only the original thread that created a view Hierarchy can touch its views. at android.view.Viewrootimpl.checkThread(Viewrootimpl.java:6556) at android.view.Viewrootimpl.focusableViewAvailable(Viewrootimpl.java:3034) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.Viewgroup.focusableViewAvailable(Viewgroup.java:761) at android.view.View.setFlags(View.java:10555) at android.view.View.setFocusable(View.java:7481) at android.widget.Adapterview.checkFocus(Adapterview.java:738) at android.widget.Absspinner.setAdapter(Absspinner.java:116) at android.widget.Spinner.setAdapter(Spinner.java:508) at android.support.v7.widget.Appcompatspinner.setAdapter(Appcompatspinner.java:391) realsysten.com.br.sigarestaurante.importaFtpActivity.listarArvosFTP(importaFtpActivity.java:97) realsysten.com.br.sigarestaurante.importaFtpActivity$3.run(importaFtpActivity.java:66) at java.lang.Thread.run(Thread.java:818)
That’s right Furflez, this is also a good alternative. Here is an example implemented for our friend see: http://answall.com/questions/130867/extraindo-arquivo-com-progressdialog/130874#130874
– Emerson JS
I would use Asynctask, but always wrong, I can’t make it work kkkkkk
– Taha tsu