Download XML from a Web Server: Android Programming

Please login in order to report media.

  • Uploaded 8 months ago in the category How to programming

    Tutorial showing how to download XML data from a sample Google AppEngine web server (http://demoprojectserver1234.appspot.com/) in an Android project.

    Full Android code below.
    Support videos via Patreon: https://www.patreon.com/DrBFraser

    // imports omitted due to YouTube space restriction!
    // Some code based on content in Android Application Development in 24 hours by Darcey and Conder (2nd ed).
    /**
    * Demonstrate querying a basic application server for information.
    *
    */
    public class MainActivity extends Activity {
    private static final String TAG = "ProjectServerDemo";

    public static final String APP_ID = "3fa19507-c746-4dfa-8ded-ec60ef4d30d9";
    public static final String SERVER_URL = "http://demoprojectserver1234.appspot.com/";
    public static final String QUERY_FILE = "xmlquery.cgi";
    public static final String QUERY_OPTIONS = "?appid=" + APP_ID;
    public static final String QUERY_URL = SERVER_URL + QUERY_FILE + QUERY_OPTIONS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    public void _ClearDisplay(View v) {
    TextView textView = (TextView) findViewById(R.id.displayData);
    textView.setText("");
    }

    public void _QueryServer(View v) {
    Log.i(TAG, "Query server...");
    AsyncDownloader downloader = new AsyncDownloader();
    downloader.execute();
    }

    private void handleNewRecord(String itemId, String data) {
    TextView textView = (TextView) findViewById(R.id.displayData);
    String message =
    textView.getText().toString() + "n" +
    itemId + ": " + data;

    textView.setText(message);
    }


    // Inner class for doing background download.
    // REVISIT:- Change the [[ to a less than; ]] to a greater than.
    // (YouTube won't let you use those characters...)
    private class AsyncDownloader extends AsyncTask[[Object, String, Integer]] {

    @Override
    protected Integer doInBackground(Object...

    arg0) {
    XmlPullParser receivedData = tryDownloadingXmlData();
    int recordsFound = tryParsingXmlData(receivedData);
    return recordsFound;
    }

    private XmlPullParser tryDownloadingXmlData() {
    try {
    Log.i(TAG, "Now downloading...");
    URL xmlUrl = new URL(QUERY_URL);
    XmlPullParser receivedData = XmlPullParserFactory.newInstance().newPullParser();
    receivedData.setInput(xmlUrl.openStream(), null);
    return receivedData;
    } catch (XmlPullParserException e) {
    Log.e(TAG, "XmlPullParserExecption", e);
    } catch (IOException e) {
    Log.e(TAG, "XmlPullParserExecption", e);
    }
    return null;
    }

    private int tryParsingXmlData(XmlPullParser receivedData) {
    if (receivedData != null) {
    try {
    return processReceivedData(receivedData);
    } catch (XmlPullParserException e) {
    Log.e(TAG, "Pull Parser failure", e);
    } catch (IOException e) {
    Log.e(TAG, "IO Exception parsing XML", e);
    }
    }
    return 0;
    }

    private int processReceivedData(XmlPullParser xmlData) throws XmlPullParserException, IOException {
    int recordsFound = 0;

    // Find values in the XML records
    String appId = ""; // Attributes
    String itemId = "";
    String timeStamp = "";
    String data = ""; // Text

    int eventType = -1;
    while (eventType != XmlResourceParser.END_DOCUMENT) {
    String tagName = xmlData.getName();

    switch (eventType) {
    case XmlResourceParser.START_TAG:
    // Start of a record, so pull values encoded as attributes.
    if (tagName.equals("record")) {
    appId = xmlData.getAttributeValue(null, "appid");
    itemId = xmlData.getAttributeValue(null, "itemid");
    timeStamp = xmlData.getAttributeValue(null, "timestamp");
    data = "";
    }
    break;

    // Grab data text (very simple processing)
    // NOTE: This could be full XML data to process.
    case XmlResourceParser.TEXT:
    data += xmlData.getText();
    break;

    case XmlPullParser.END_TAG:
    if (tagName.equals("record")) {
    recordsFound++;
    publishProgress(appId, itemId, data, timeStamp);
    }
    break;
    }
    eventType = xmlData.next();
    }

    // Handle no data available: Publish an empty event.
    if (recordsFound == 0) {
    publishProgress();
    }
    Log.i(TAG, "Finished processing "+recordsFound+" records.");
    return recordsFound;
    }

    @Override
    protected void onProgressUpdate(String...

    values) {
    if (values.length == 0) {
    Log.i(TAG, "No data downloaded");
    }
    if (values.length == 4) {
    String appId = values[0];
    String itemId = values[1];
    String data = values[2];
    String timeStamp = values[3];

    // Log it
    Log.i(TAG, "AppID: " + appId + ", Timestamp: " + timeStamp);
    Log.i(TAG, " ItemID: " + itemId + ", Data: " + data);

    // Pass it to the application
    handleNewRecord(itemId, data);

    }
    super.onProgressUpdate(values);
    }
    }
    }

  • downloadxmlwebserver:androidprogramming