1

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

这基本上调用数据API和负荷为给定的搜索词视频50到ListView中

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). 
+0

你有哪些YouTube的DLL的版本? – DaImTo

+0

版本3是我所拥有的 –

回答

0

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

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

我会尝试在一个try/catch包裹整个套路只是为了确保你没有得到一个403点或其它接入/ AUTH错误:

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); 
} 
+0

我试过了,它没有捕获任何东西。让它继续运行,它一直在循环,我在上面发布了堆栈跟踪。 –

+1

如果你在'var searchListRequest ...'上设置断点,它是否曾经到过那里?你如何调用你的'Run'方法? – SushiHangover

+0

它到达那里,但永远不会通过那里; foreach循环没有到达。我有另一种方法,它是一个按钮点击的事件处理程序,里面是Run()。 –