0

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!

CodeNinja
  • 836
  • 1
  • 15
  • 38

1 Answers1

1

The important part that you are missing is this (from the Android docs):

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }

You need to parse your XML and add it to your UI in the above function. Since onPostExecute runs on the UI Thread.

It might be that you need to pass a callback interface into the AsyncTask constructor to be able to send the parsed data back to your Activity.

For example:

public class XmlManager extends AsyncTask<String, String, String> {
    private MyCallback mCallback = null;

    public XmlManager(MyCallback callback) {
        mCallback = callback;
    }

    ...

    protected void onPostExecute(String result) {
        mCallback.onResult(result);
    }
}
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • Ok, YES i got it working, my DataLayerClass starts the async proces and it gets the result back over the onPostExecute => mCallBack.onResult(....){//do something} method. But now can't update my UI from here. Can i catch the event in the activity who manages the UI too? i mean the mCallBack.onResult(String result) { //Do something with result }, i cant set implement mCallBack behind the extends AppCompatActivity – CodeNinja Jan 22 '16 at 13:10
  • Just pass the interface through. – Knossos Jan 22 '16 at 13:18