TJB
TJB

Reputation: 13497

MonoDroid ObjectDisposedException when loading XDocument for RSS

I'm fiddling with MonoDroid (aka Mono for Android) trying to build a simple podcast downloading app, currently just testing out retrieval of an RSS feed contents.

My simple app is:

When a user clicks on download, I fetch the RSS contents, currently using XDocument (I also tried with just WebRequest / Streams etc.), here's the short version:

void button_Click(object sender, EventArgs e)
{
    output.Text += "user input: " + rssUriInput.Text + "\n";

    try
    {
        output.Text += "### document ###";
        output.Text += XDocument.Load(rssUriInput.Text).ToString();
        output.Text += "### document ###";
    }
    catch (Exception ex)
    {
        output.Text += "OOOPS something went wrong:\n" + ex.ToString();
    }
}

I've tried several approaches, but keep getting ObjectDisposedExceptions when I do XDocument.Load(/*my uri here*/);

My AssemblyInfo.cs includes the

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]

Now, disclaimer, after getting hello world running I kind of went off on my own, so I may be missing some MonoDroid fundamentals, but this code works in a normal C# app (I tested it) but
Why am I doing wrong here? Do I have to do something special to use xml / linq / network access in MonoDroid?

here is the full activity code:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Xml.Linq;

namespace RssDownlowd
{
    [Activity(Label = "RssDownlowd", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        EditText rssUriInput;
        Button button;
        TextView output;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.RssDownload);

            rssUriInput = FindViewById<EditText>(Resource.Id.rssUriText);
            button = FindViewById<Button>(Resource.Id.downloadButton);
            output = FindViewById<TextView>(Resource.Id.outputTextView);

            button.Click += new EventHandler(button_Click);
        }

        void button_Click(object sender, EventArgs e)
        {
            output.Text += "user input: " + rssUriInput.Text + "\n";

            try
            {
                output.Text += "### document ###";
                output.Text += XDocument.Load(rssUriInput.Text).ToString();
                output.Text += "### document ###";
            }
            catch (Exception ex)
            {
                output.Text += "OOOPS something went wrong:\n" + ex.ToString();
            }
        }

    }
}

And here is the full exception:

"System.ObjectDisposedException: The object was used after being disposed.

  at System.Net.WebConnection.BeginRead (System.Net.HttpWebRequest request, System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0 

  at System.Net.WebConnectionStream.BeginRead (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in <filename unknown>:0 

  at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0 

  at System.Xml.XmlInputStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 

  at System.Xml.NonBlockingStreamReader.ReadBuffer () [0x00000] in <filename unknown>:0 

  at System.Xml.NonBlockingStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0 

  at System.Xml.XmlStreamReader.Read (System.Char[] dest_buffer, Int32 index, Int32 count) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadTextReader (Int32 remained) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.PeekChar () [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadChar () [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.Expect (Int32 expected) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadEntityReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadReference (Boolean ignoreEntityReferences) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.ReadContent () [0x00000] in <filename unknown>:0 

  at Mono.Xml2.XmlTextReader.Read () [0x00000] in <filename unknown>:0 

  at System.Xml.XmlTextReader.Read () [0x00000] in <filename unknown>:0 

  at Mono.Xml.XmlFilterReader.Read () [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options) [0x00000] in <filename unknown>:0 

  at System.Xml.Linq.XDocument.Load (System.String uri) [0x00000] in <filename unknown>:0 

  at RssDownlowd.Activity1.button_Click (System.Object sender, System.EventArgs e) [0x0004a] in C:\\Users\\khd483\\code\\HelloMonoDroid\\RssDownlowd\\Activity1.cs:47 "

Upvotes: 2

Views: 1064

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24470

Strangely enough when I tried your code it worked just fine:

Screen shot

Have you enabled Linking in your project in the Mono for Android options?

Also consider doing an async download of the RSS as it blocks the UI Thread until it is done, making the app unresponsive.

Upvotes: 1

Related Questions