I'm trying to make my application download an XML (RSS Feed) and read its tags to display it on the home screen. However, the application closes after downloading before attempting to start the onPostExecute method of AsyncTask getFeed and AsyncTask readXML of Tags). And if I try to just check the application version, I use AsyncTask to download the file and read its contents, check the version and ask the user if he wants to perform the application update. However, if I have already run AsyncTask UpdateApp , I can not run the low APK without the application closing.
Complete Code: Pastebin
AsyncTask<Void, Void, String> getFeed = new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
String retorno = "não";
try {
// Baixar Feed
URL url = new URL("http://www.ipoema.tumblr.com/rss");
File file = new File(PATH + "iPoema.xml");
file.delete();
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
lenghtOfFile = ucon.getContentLength();
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
progress = current;
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
retorno = "sim";
} catch (IOException e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar o download do arquivo.");
}
return retorno;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.equals("sim")) {
try {
lerXML.execute();
} catch (Exception e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar a leitura do arquivo.");
}
}
}
};
AsyncTask<Void, Void, String> lerXML = new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
String state = "erro";
try {
// Ler XML
showMessage("iPoema", "Arquivo lido com sucesso");
File f = new File(PATH + "iPoema.xml");
SAXBuilder sb = new SAXBuilder();
Document d = sb.build(f);
Element mural = d.getRootElement();
List<Element> elements = mural.getChildren("channel");
Iterator<Element> i = elements.iterator();
int x = 0;
//for (int x = 0; x <= elements.size(); x++) {
while (i.hasNext()) {
//Element node = (Element) elements.get(x);
Element node = (Element) i.next();
Element item = node.getChild("item");
titulo[x] = item.getChildText("title");
texto[x] = item.getChildText("description");
autor[x] = item.getChildText("category");
x++;
}
state = "ok";
} catch (JDOMException e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar a leitura do XML.");
} catch (IOException e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar a leitura do XML.");
}
return state;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s.equals("ok")) {
try {
// Títulos
TextView titulo1 = (TextView) findViewById(R.id.titulo1);
TextView titulo2 = (TextView) findViewById(R.id.titulo2);
TextView titulo3 = (TextView) findViewById(R.id.titulo3);
TextView titulo4 = (TextView) findViewById(R.id.titulo4);
// Textos
TextView texto1 = (TextView) findViewById(R.id.texto1);
TextView texto2 = (TextView) findViewById(R.id.texto2);
TextView texto3 = (TextView) findViewById(R.id.texto3);
TextView texto4 = (TextView) findViewById(R.id.texto4);
// SetText
titulo1.setText(getTitulo(0));
texto1.setText(getTexto(0)+"\n\n#"+getAutor(0)+"\n\n");
titulo2.setText(getTitulo(1));
texto2.setText(getTexto(1)+"\n\n#"+getAutor(1)+"\n\n");
titulo3.setText(getTitulo(2));
texto3.setText(getTexto(2)+"\n\n#"+getAutor(2)+"\n\n");
titulo4.setText(getTitulo(3));
texto4.setText(getTexto(3)+"\n\n#"+getAutor(3)+"\n\n");
// Exibe
titulo1.setVisibility(TextView.VISIBLE);
texto1.setVisibility(TextView.VISIBLE);
titulo2.setVisibility(TextView.VISIBLE);
texto2.setVisibility(TextView.VISIBLE);
titulo3.setVisibility(TextView.VISIBLE);
texto3.setVisibility(TextView.VISIBLE);
titulo4.setVisibility(TextView.VISIBLE);
texto4.setVisibility(TextView.VISIBLE);
} catch (Exception e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar a leitura do arquivo.");
}
}
}
};
AsyncTask<Void, Void, String> atualizaApp = new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... params) {
String versao = "0";
try {
// Baixar iPoema.txt
URL url = new URL("http://www.xadees.xpg.com.br/iPoema.txt");
File file = new File(PATH + "iPoema.txt");
file.delete();
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
lenghtOfFile = ucon.getContentLength();
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
progress = current;
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
// Ler iPoema.txt
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
}
versao = text.toString();
} catch (IOException e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar o download do arquivo.");
} catch (Exception e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar a leitura do arquivo.");
}
return versao;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
int vAtual = Pi.versionCode;
int vAPK = Integer.parseInt(s);
if (vAtual < vAPK) {
try {
// Baixar
URL url = new URL("http://www.xadees.xpg.com.br/iPoema%20for%20Android.apk");
File file = new File(PATH + "iPoema.apk");
file.delete();
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
lenghtOfFile = ucon.getContentLength();
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
progress = current;
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
// Instalar
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH + "iPoema.apk")), "application/vnd.android.package-archive");
//java.lang.Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", "adb install -r " + PATH + "iPoema.apk"});
//proc.waitFor();
startActivityForResult(intent,0);
} catch (IOException e) {
showMessage(e.getClass().getName(), "Erro ao tentar realizar o download do pacote.");
}
} else {
showMessage("iPoema", "iPoema já está atualizado.");
}
}
};