Jay Douglass
Jay Douglass

Reputation: 4918

MonoDroid & RestSharp, MethodMissingException

I'm trying to use RestSharp with MonoDroid and I'm running into some problems. A MethodMissingException is thrown with the message "Default constructor not found for type EmPubLite.Android.Tweet[]." I can only see the exception when I inspect the Task in the VS debugger, it doesn't show up in LogCat. I know that the exception is related to the JSON deserialization in RestSharp.

The linker is set to 'None' in MonoDroid options, and I also tried adding the [Preserve] attribute to my DTOs.

Beyond getting this to work, why doesn't this crash and show up in LogCat? I'd like any Tasks that fail with exceptions to rethrow the exception when I access the result.

[Activity(Label = "EmPubLite", MainLauncher = true, Icon = "@drawable/icon")]
public class EmPubLiteActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.EmPubLight);

        var client = new TwitterClient();
        client.Search("bielema").ContinueWith(tweets =>
            {
                Debug.WriteLine("back");
                Debug.WriteLine(tweets.Result[0].Text); // Exception not showing up in LogCat
            });
    }
}

public class TwitterClient
{
    private RestClient client = new RestClient();

    public Task<Tweet[]> Search(string query)
    {
        var request = new RestRequest("http://search.twitter.com/search.json?rpp=5&include_entities=true&result_type=mixed&q={q}", Method.GET);
        request.AddUrlSegment("q", query);
        var taskCompletionSource = new TaskCompletionSource<Tweet[]>();
        client.ExecuteAsync<SearchResult>(request, response =>
        {
            if (response.ErrorException != null) taskCompletionSource.TrySetException(response.ErrorException);
            else taskCompletionSource.TrySetResult(response.Data.Results);

            // taskCompletionSource.TrySetResult(response.Data.Results);
        });
        return taskCompletionSource.Task;
    }
}

public class SearchResult
{
    public Tweet[] Results { get; set; }
}

public class Tweet
{
    public string Text { get; set; }
}

Upvotes: 1

Views: 212

Answers (1)

Jay Douglass
Jay Douglass

Reputation: 4918

I fixed this by using the ServiceStack JSON parser instead of the default RestSharp JSON parser.

Upvotes: 1

Related Questions