异步任务超时

时间:2016-09-15 23:53:25

标签: xamarin xamarin.ios xamarin.android youtube-data-api google-api-dotnet-client

我正在Xamarin中编写一个页面,该页面使用YouTube Data API显示YouTube搜索。它编译得很好,但是当我在我的Android设备上运行时,它会冻结,并在一段时间后提示退出应用程序,因为它没有响应。我认为它与建立YouTubeService对象有关。

这基本上调用数据API并将给定搜索词的50个视频加载到列表视图

private async Task Run()
    {
        videoList.Clear();

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            ApiKey = DEVELOPER_KEY,
            ApplicationName = "Xamarin-Basics"
        });

        var searchListRequest = youtubeService.Search.List("snippet");
        searchListRequest.Q = "mkbhd";
        searchListRequest.MaxResults = 50;

        var searchListResponse = await searchListRequest.ExecuteAsync();

        foreach(var searchResult in searchListResponse.Items)
        {
            if (searchResult.Id.Kind.Equals("youtube#video"))
            {
                videoList.Add(new VideoInfo(searchResult.Snippet.Title, searchResult.Snippet.Description));
            }
        }
    }

此外,这里是堆栈跟踪,似乎一遍又一遍地在线程中运行操作:

09-15 20:31:21.352 D/Mono    ( 6831): Assembly Ref addref     Newtonsoft.Json[0xb72c8210] -> System.Linq.Expressions[0xb72e98a0]: 3
Loaded assembly: Anonymously Hosted DynamicMethods Assembly [External]
09-15 20:31:21.562 D/Mono    ( 6831): Assembly Ref addref     Google.Apis.YouTube.v3[0xb72c7310] -> Newtonsoft.Json[0xb72c8210]: 4
09-15 20:31:22.127 D/Mono    ( 6831): DllImport searching in: '__Internal'     ('(null)').
09-15 20:31:22.127 D/Mono    ( 6831): Searching for 'CloseZStream'.
09-15 20:31:22.127 D/Mono    ( 6831): Probing 'CloseZStream'.
09-15 20:31:22.127 D/Mono    ( 6831): Found as 'CloseZStream'.
09-15 20:31:31.950 D/Mono    ( 6831): [0xb76fa5b0] worker finishing
Thread finished: <Thread Pool> #3
The thread 'Unknown' (0x3) has exited with code 0 (0x0).
09-15 20:31:57.148 D/Mono    ( 6831): [0xb808e9b8] worker finishing
Thread finished: <Thread Pool> #7
The thread 'Unknown' (0x7) has exited with code 0 (0x0).
09-15 20:32:04.446 D/Mono    ( 6831): [0xb77219d8] worker finishing
Thread finished: <Thread Pool> #4
The thread 'Unknown' (0x4) has exited with code 0 (0x0).
Thread finished: <Thread Pool> #2
The thread 'Unknown' (0x2) has exited with code 0 (0x0).
09-15 20:32:49.051 D/Mono    ( 6831): [0xb808de40] worker finishing
Thread finished: <Thread Pool> #8
The thread 'Unknown' (0x8) has exited with code 0 (0x0).
Thread started: <Thread Pool> #11
Thread started: <Thread Pool> #12
09-15 20:32:59.725 D/Mono    ( 6831): [0xb81d8738] worker starting
09-15 20:33:30.573 D/Mono    ( 6831): [0xb81d8738] worker finishing
Thread finished: <Thread Pool> #12
The thread 'Unknown' (0xc) has exited with code 0 (0x0).
09-15 20:33:46.394 D/Mono    ( 6831): [0xb80a4790] worker finishing
Thread finished: <Thread Pool> #9
The thread 'Unknown' (0x9) has exited with code 0 (0x0). 
Thread finished: <Thread Pool> #11
The thread 'Unknown' (0xb) has exited with code 0 (0x0).
Thread started: <Thread Pool> #13
Thread started: <Thread Pool> #14
09-15 20:34:39.731 D/Mono    ( 6831): [0xb81d8738] worker starting
Thread started: <Thread Pool> #15
09-15 20:34:39.749 D/Mono    ( 6831): [0xb84d0f60] worker starting
09-15 20:34:52.494 D/Mono    ( 6831): [0xb84d0f60] worker finishing
Thread finished: <Thread Pool> #15
The thread 'Unknown' (0xf) has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:0)

您的代码看起来基本正确。我假设您使用的是最新的v3 api:

Google.Apis.YouTube.v3" version="1.16.0.582"

我会尝试在try / catch中包装整个例程,以确保您没有获得403或其他访问/身份验证错误:

Xamarin.Android示例:

try
{
    var youtubeService = new YouTubeService(new BaseClientService.Initializer()
    {
        ApiKey = DEVELOPER_KEY,
        ApplicationName = "com.sushihangover.youtubeapi",
    });
    var searchListRequest = youtubeService.Search.List("snippet");
    searchListRequest.Q = "StackOverflow";
    searchListRequest.MaxResults = 50;

    var searchListResponse = await searchListRequest.ExecuteAsync();

    foreach (var searchResult in searchListResponse.Items)
    {
        if (searchResult.Id.Kind.Equals("youtube#video"))
        {
            Console.WriteLine(searchResult.Snippet.Title);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}