MonoDroid& RestSharp,MethodMissingException

时间:2012-12-31 20:54:08

标签: xamarin.android restsharp

我正在尝试将RestSharp与MonoDroid一起使用,我遇到了一些问题。抛出MethodMissingException,并显示消息“找不到类型EmPubLite.Android.Tweet []的默认构造函数。”我在VS调试器中检查任务时只能看到异常,它不会显示在LogCat中。我知道该异常与RestSharp中的JSON反序列化有关。

MonoDroid选项中的链接器设置为“None”,我也尝试将[Preserve]属性添加到我的DTO中。

除了让它工作之外,为什么这个崩溃并不会出现在LogCat中?我希望任何因异常而失败的任务在我访问结果时重新抛出异常。

[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; }
}

1 个答案:

答案 0 :(得分:1)

我通过使用ServiceStack JSON解析器而不是默认的RestSharp JSON解析器来解决这个问题。