Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created July 15, 2016 15:19
Show Gist options
  • Save alvareztech/5f8383227493b9225014c5c9bf134f0e to your computer and use it in GitHub Desktop.
Save alvareztech/5f8383227493b9225014c5c9bf134f0e to your computer and use it in GitHub Desktop.
Ejemplo obtener XML, AsyncTask. Curso de desarrollo de aplicaciones Android.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tech.alvarez.ejemploxml">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package tech.alvarez.ejemploxml.models;
import java.util.Date;
/**
* Created by Daniel Alvarez on 14/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class Evento {
private String nombre;
private String lugar;
private Date fecha;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getLugar() {
return lugar;
}
public void setLugar(String lugar) {
this.lugar = lugar;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
}
package tech.alvarez.ejemploxml;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.ArrayList;
import tech.alvarez.ejemploxml.models.Evento;
import tech.alvarez.ejemploxml.utils.ParserXML;
public class MainActivity extends AppCompatActivity {
private ArrayList<Evento> eventos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "https://docs.google.com/uc?export=download&id=0B5wCprFP-EBZd29KNjNlaDFfOFk";
ObtenerXMLTask obtenerXMLTask = new ObtenerXMLTask();
obtenerXMLTask.execute(url);
}
public class ObtenerXMLTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
String url = strings[0];
ParserXML parserXML = new ParserXML(url);
eventos = parserXML.parse();
return null;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
for (int i = 0; i < eventos.size(); i++) {
Evento e = eventos.get(i);
Log.i("MIAPP", "Evento");
Log.i("MIAPP", " Nombre: " + e.getNombre());
Log.i("MIAPP", " Lugar: " + e.getLugar());
Log.i("MIAPP", " Fecha: " + e.getFecha());
}
}
}
}
package tech.alvarez.ejemploxml.utils;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import tech.alvarez.ejemploxml.models.Evento;
/**
* Created by Daniel Alvarez on 14/7/16.
* Copyright © 2016 Alvarez.tech. All rights reserved.
*/
public class ParserXML {
private URL url;
private Evento eventoActual;
public ParserXML(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public ArrayList<Evento> parse() {
final ArrayList<Evento> eventos = new ArrayList<Evento>();
RootElement root = new RootElement("eventos");
Element item = root.getChild("evento");
item.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
eventoActual = new Evento();
}
});
item.getChild("nombre").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
eventoActual.setNombre(s);
}
});
item.getChild("lugar").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
eventoActual.setLugar(s);
}
});
item.getChild("fecha").setEndTextElementListener(new EndTextElementListener() {
@Override
public void end(String s) {
eventoActual.setFecha(convertir(s));
}
});
item.setEndElementListener(new EndElementListener() {
@Override
public void end() {
eventos.add(eventoActual);
}
});
try {
Xml.parse(getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return eventos;
}
private InputStream getInputStream() {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
// Método extra
public static Date convertir(String fechaString) {
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = null;
try {
date = format.parse(fechaString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment