1

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

链接器在 MonoDroid 选项中设置为“无”,我还尝试将 [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; }
}
4

1 回答 1

1

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

于 2012-12-31T23:21:08.693 回答