[Android Studio] albertAH2020:
Hello, I am learning to program in android and I have the following project. I need to download two XML files from the internet, these files correspond to fuel prices and the location of gas stations. The location and price come in different files, to join these files I need to identify the id of the service station and create a new file with the location and price of the fuel of each station. I am learning how to perform each of these steps. In the code that I add below I want to download a file, save it in memory and display it in TextView. The code does not work correctly, I manage to download the file but I cannot save it in memory to later show it in the Textview. The code downloads the data in the / data / folder, in order to verify that the connection is made and the data is downloaded. How can I download a file and save it in memory to later parse the XML?

Button button=(Button) findViewById(R.id.DownloadBt);

txt = (TextView) findViewById(R.id.txt);

button.setOnClickListener(this);

}

[MENTION=439709]override[/MENTION]

public void onClick(View view){

switch (view.getId()){

case R.id.DownloadBt:

descargarPDF();

break;

}

}

void descargarPDF(){

String urlDescargarPlaces = "https://publicacionexterna.azurewebsites.net/publicaciones/places";

//String urlDescargarPrices = "https://publicacionexterna.azurewebsites.net/publicaciones/prices";

DescargarPDFAsyncTask descargarPlaces=new DescargarPDFAsyncTask("places.xml");

//DescargarPDFAsyncTask descargarPrices=new DescargarPDFAsyncTask("prices.xml");

descargarPlaces.execute(urlDescargarPlaces);

//descargarPrices.execute(urlDescargarPrices);

}

class DescargarPDFAsyncTask extends AsyncTask<String, Integer, String> {

String mFileName;

XMLParse xmlParse=new XMLParse();

DescargarPDFAsyncTask(String fileName){

mFileName=fileName;

}

[MENTION=439709]override[/MENTION]

protected void onPreExecute(){

super.onPreExecute();

}

[MENTION=439709]override[/MENTION]

protected String doInBackground(String... urlFichero) {

String urlADescargar=urlFichero[0];

HttpURLConnection conexion=null;

InputStream input=null;

OutputStream output=null;

try {

URL url = new URL(urlADescargar);

conexion = (HttpURLConnection) url.openConnection();

conexion.connect();

if(conexion.getResponseCode() != HttpURLConnection.HTTP_OK) {

return "Connection not successful";

}

input=conexion.getInputStream();

String rutaFicheroGuardado=getFilesDir()+ "/"+mFileName; // /data/data/com.exmaple.downloadurlfile/places.xml.

output=new FileOutputStream(rutaFicheroGuardado);

byte[] data=new byte[1024];

int count=0;

while((count=input.read(data)) >0){//!=-1;

output.write(data,0, count);

}

byteArrayStream = new ByteArrayInputStream(data);//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

} catch (MalformedURLException ex) {

ex.printStackTrace();

return "Error: "+ ex.getMessage();

} catch (IOException ex) {

ex.printStackTrace();

return "Error: "+ex.getMessage();

} finally{

{

try {

if(input!=null) input.close();

if(output!=null) output.close();

if(conexion!=null) conexion.disconnect();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return "Connection successful.";

}

[MENTION=439709]override[/MENTION]

protected void onProgressUpdate(Integer...values){

super.onProgressUpdate(values);

}

[MENTION=439709]override[/MENTION]

protected void onPostExecute(String mensaje){

super.onPostExecute(mensaje);

XMLParse xmlParse= new XMLParse();

xmlParse.parseXML();

Toast.makeText(getApplicationContext(),mensaje,Toast.LENGTH_LONG).show();

}

}

public class XMLParse {

StringBuilder result;

XMLParse(){

}

public void parseXML() {

XmlPullParserFactory parserFactory;

try {

parserFactory = XmlPullParserFactory.newInstance();

XmlPullParser parser = parserFactory.newPullParser();

parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);

parser.setInput(byteArrayStream,null);//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

processParsing(parser);

} catch (XmlPullParserException e) {

} catch (IOException e) {

}

}

private void processParsing(XmlPullParser parser) throws IOException, XmlPullParserException{

ArrayList<Player> players = new ArrayList<>();

int eventType = parser.getEventType();

Player currentPlayer = null;

while (eventType != XmlPullParser.END_DOCUMENT) {

String eltName = null;

switch (eventType) {

case XmlPullParser.START_TAG:

eltName = parser.getName();

if ("place".equals(eltName)) {

currentPlayer = new Player();

players.add(currentPlayer);

} else if (currentPlayer != null) {

if ("name".equals(eltName)) {

currentPlayer.name = parser.nextText();

} else if ("cre_id".equals(eltName)) {

currentPlayer.cre_id = parser.nextText();

} else if ("x".equals(eltName)) {

currentPlayer.x = parser.nextText();

} else if("y".equals(eltName)){

currentPlayer.y = parser.nextText();

}

}

break;

}

eventType = parser.next();

}

printPlayers(players);

}

private void printPlayers(ArrayList<Player> players) {

StringBuilder builder = new StringBuilder();

for (Player player : players) {

builder.append(player.name).append("\n").

append(player.cre_id).append("\n").

append(player.x).append("\n").

append(player.y).append("\n\n");

}

txt.setText(builder.toString());

}

}

}