02-02 11:34:38.472 3867-3867/com.mimapa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mimapa, PID: 3867
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mimapa/com.mimapa.CrearModificar}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getByName(InetAddress.java:708)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at com.utils.FtpControl.conectar(FtpControl.java:101)
at com.mimapa.CrearModificar.leerArchivos(CrearModificar.java:65)
at com.mimapa.CrearModificar.onCreate(CrearModificar.java:39)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
02-02 11:34:38.472 3867-3867/com.mimapa E/AndroidRuntime: FATAL EXCEPTION: main Process: com.mimapa, PID: 3867 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mimapa/com.mimapa.CrearModificar}: android.os.NetworkOnMainThreadException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) at java.net.InetAddress.getByName(InetAddress.java:708) at org.apache.commons.net.SocketClient.connect(SocketClient.java:203) at com.utils.FtpControl.conectar(FtpControl.java:101) at com.mimapa.CrearModificar.leerArchivos(CrearModificar.java:65) at com.mimapa.CrearModificar.onCreate(CrearModificar.java:39) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
public class MainActivity extends AppCompatActivity {
Button btMapas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btMapas = (Button) findViewById(R.id.btMapa);
btMapas.setEnabled(true);
leerArchivos();
}
private void leerArchivos() {
BufferedReader br = null;
FtpControl ftpControl = new FtpControl("localhost", "userftp", "admin");
ftpControl.conectar();
String[] lista = ftpControl.listaArchivos("/srv/ftp/");
try {
for (int i = 0; i < lista.length; i++) {
br = ftpControl.descargarArchivo("/srv/ftp/" + lista[i]);
if (br != null) {
guardarUsuario(
br.readLine(),
br.readLine(),
br.readLine(),
br.readLine()
);
}
br.close();
}
ftpControl.cerrar();
} catch (Exception e) {
e.printStackTrace();
}
}
private void guardarUsuario(String nombre, String lat, String log, String fecha) {
MyDataBase admin = new MyDataBase(this,
null, null, 1);
ContentValues values = new ContentValues();
// Pares clave-valor
values.put(UsuarioContract.UsuarioEntry.ORGANIZACION, "Grupo A");
values.put(UsuarioContract.UsuarioEntry.USUARIO_APP, 0);
values.put(UsuarioContract.UsuarioEntry.USUARIO, nombre);
values.put(UsuarioContract.UsuarioEntry.LONGITUD, log);
values.put(UsuarioContract.UsuarioEntry.LATITUD, lat);
values.put(UsuarioContract.UsuarioEntry.ACTIVADO, 0);
Controlador.guardarUsuario(admin, values);
}
Clase conexion ftp:
public class FtpControl {
// datos
private String servidor;
private String usuario;
private String pass;
private FTPClient cliente = null;
public FtpControl(String servidor, String usuario, String pass) {
this.servidor = servidor;
this.usuario = usuario;
this.pass = pass;
}
public boolean subirArchivo(String path, String nombre) {
FileInputStream fis = null;
// si el usuario esta conectado al servidor
if (cliente.isConnected()) {
try {
cliente.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
cliente.setFileTransferMode(FTP.BINARY_FILE_TYPE);
cliente.enterLocalPassiveMode();
fis = new FileInputStream(path);
cliente.storeFile(nombre, fis);
cerrar(); // cerrar sesion
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
public String[] listaArchivos(String path) {
String[] lista = null;
try {
FTPFile[] ftpFiles = cliente.listFiles(path);
int length = ftpFiles.length;
lista = new String[length];
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
lista[i] = name;
} else {
lista[i] = name;
}
}
return lista;
} catch (Exception e) {
e.printStackTrace();
}
return lista;
}
public BufferedReader descargarArchivo(String path) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(path);
br = new BufferedReader(fr);
} catch (Exception e) {
e.printStackTrace();
}
return br;
}
public void conectar() {
new Connection().execute();
}
public void cerrar() {
try {
cliente.logout();
cliente.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
private class Connection extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
cliente = new FTPClient();
try {
// conectar al servidor
cliente.connect(servidor, 21);
cliente.login(usuario, pass);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
}