I'am new with android an read a lot about the life cycles and so on on developer.android.com Now i wanna build an app that gets data from an online XML feed.
I created an XmlManager class which extends the aSyncTask<...> Here i download the XML from the URL and put it in an string, this works!
Now i will invoke the download from the main activity in the onCreate so it downloads the XML in the background. The user needs to see an tekst, "loading" or something(in an TextView). When the download (the aSync proces) is ready it need needs to fire an event or something and push?/send? the data to the activity so the activity can replace the loadingtext by the XMl string.
When i know how to do this i can find out by my self how to parse the xml and put it in an nice userinterface. I tryed to implement this but i cant get it done.
Is there anyone who can help me ? A sample code will be nice!
An simple user interface (Textview and button) and when i click the button the the background proces is started an downloads the xml and replace the current text in the textview by the XML would be GREAT!!!
The code i use for download the XML is:
public class XmlManager extends AsyncTask<String, String, String> {
String xml = null;
@Override
protected String doInBackground(String... inputUrl) {
Log.i("Status", "ASync proces started");
getStream(inputUrl[0]);
Log.i("Status", "ASync proces finished");
return xml;
}
private void getStream(String urlString) {
URL url = null;
HttpURLConnection urlConnection = null;
InputStream is = null;
try {
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
Log.i("Status", "Connection opened");
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
Log.i("Status", "XML loaded into string");
} catch (Exception e) {
Log.e("Error", e.getMessage().toString());
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
Log.i("Status", "Connection disconnected");
}
}
}
}
Thanks in advance!