我有一个歌词应用程序,需要从Spotify库中的所有歌曲中下载歌词。该过程具有3个功能:第一个调用Spotify API以检索歌曲列表并将其保存到列表中;第二个调用Genius API来搜索他们的数据库,并尝试查找标题和艺术家匹配的歌曲;第三个再次调用Genius API,以获取歌曲详细信息,例如专辑名称和特色歌手,还加载包含歌词的页面,然后将所有内容保存到文件中。
问题是由于某种原因,只要JSON解析器(来自JSON.NET)找到空值,它就会冻结整个任务,而不会引发异常或任何异常。我的主活动中基本上有相同的代码实现,只要有null值,它就只会返回一个空字符串。不管出于什么原因,这次它只是拒绝继续前进。
这是第三个函数的一部分代码:
private async Task getDetails(string APIPath)
{
Log.WriteLine(LogPriority.Info, "SmartLyrics", "getDetails (SpotifyDownload): Starting getDetails operation");
string results = await Genius.GetSongDetails(APIPath, "Bearer my_apps_auth_code"); //This method gives a "Reading content stream..." message
JObject parsed = JObject.Parse(results); //on the log when it recieves a JSON from the API
Song song = new Song() //This is a custom class to store song information throught the app.
{
title = (string)parsed["response"]["song"]["title"],
artist = (string)parsed["response"]["song"]["primary_artist"]["name"],
album = (string)parsed["response"]["song"]["album"]["name"],
header = (string)parsed["response"]["song"]["header_image_url"],
cover = (string)parsed["response"]["song"]["song_art_image_url"],
APIPath = (string)parsed["response"]["song"]["api_path"],
path = (string)parsed["response"]["song"]["path"]
}; //This is where it gets stuck. You can see in the log file that it recieves the JSON from the API but never processes it.
Log.WriteLine(LogPriority.Debug, "SmartLyrics", "getDetails (SpotifyDownload): Created new Song variable");
if (parsed["response"]["song"]["featured_artists"].HasValues)
{
Log.WriteLine(LogPriority.Info, "SmartLyrics", "getDetails (SpotifyDownload): Track has featured artists");
//...
}
else
{
Log.WriteLine(LogPriority.Info, "SmartLyrics", "getDetails (SpotifyDownload): Track does not have featured artists");
song.featuredArtist = "";
}
string downloadedLyrics;
HtmlWeb web = new HtmlWeb();
Log.WriteLine(LogPriority.Debug, "SmartLyrics", "getDetails (SpotifyDownload): Trying to load page");
var doc = await web.LoadFromWebAsync("https://genius.com" + song.path);
Log.WriteLine(LogPriority.Verbose, "SmartLyrics", "getDetails (SpotifyDownload): Loaded Genius page");
var lyricsBody = doc.DocumentNode.SelectSingleNode("//div[@class='lyrics']");
downloadedLyrics = Regex.Replace(lyricsBody.InnerText, @"^\s*", ""); //these regexes just removes some
downloadedLyrics = Regex.Replace(downloadedLyrics, @"[\s]+$", ""); //whitespace on the start and end of the lyrics
song.lyrics = downloadedLyrics;
Log.WriteLine(LogPriority.Info, "SmartLyrics", "getDetails (SpotifyDownload): Completed getDetails task for " + song.APIPath);
}
此方法连续被调用3次,还有另一个函数在再次调用3次之前检查是否完成了最后3次调用。
这是LogCat:
06-26 14:26:21.189: I/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): No tasks are running!
06-26 14:26:21.189: I/SmartLyrics(31087): getDetails (SpotifyDownload): Starting getDetails operation
06-26 14:26:21.190: V/SmartLyrics(31087): GeniusRequests.cs: Adding Auth headers to HttpClient
06-26 14:26:21.190: W/SmartLyrics(31087): Url sent to HttpClient: https://api.genius.com/songs/3393276
06-26 14:26:21.193: I/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): foreach for index 4 completed.
06-26 14:26:21.193: I/SmartLyrics(31087): getDetails (SpotifyDownload): Starting getDetails operation
06-26 14:26:21.194: V/SmartLyrics(31087): GeniusRequests.cs: Adding Auth headers to HttpClient
06-26 14:26:21.194: W/SmartLyrics(31087): Url sent to HttpClient: https://api.genius.com/songs/3125896
06-26 14:26:21.195: I/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): foreach for index 5 completed.
06-26 14:26:21.196: I/SmartLyrics(31087): getDetails (SpotifyDownload): Starting getDetails operation
06-26 14:26:21.196: V/SmartLyrics(31087): GeniusRequests.cs: Adding Auth headers to HttpClient
06-26 14:26:21.196: W/SmartLyrics(31087): Url sent to HttpClient: https://api.genius.com/songs/2822309
06-26 14:26:21.198: I/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): foreach for index 6 completed. //All three calls made, now it will wait until all of them are finished to call three more
06-26 14:26:21.198: E/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): getDetails tasks still running //This is from a while loop that checks if all tasks have finished.
06-26 14:26:21.958: V/SmartLyrics(31087): GeniusRequests.cs: Reading content stream... //1
06-26 14:26:21.968: D/SmartLyrics(31087): getDetails (SpotifyDownload): Created new Song variable
06-26 14:26:21.968: I/SmartLyrics(31087): getDetails (SpotifyDownload): Track does not have featured artists
06-26 14:26:21.968: D/SmartLyrics(31087): getDetails (SpotifyDownload): Trying to load page
06-26 14:26:21.972: V/SmartLyrics(31087): GeniusRequests.cs: Reading content stream... //2
06-26 14:26:22.536: V/SmartLyrics(31087): GeniusRequests.cs: Reading content stream... //3 This means that all 3 of them recieved a response from the API
06-26 14:26:22.540: D/SmartLyrics(31087): getDetails (SpotifyDownload): Created new Song variable
06-26 14:26:22.540: I/SmartLyrics(31087): getDetails (SpotifyDownload): Track does not have featured artists
06-26 14:26:22.540: D/SmartLyrics(31087): getDetails (SpotifyDownload): Trying to load page
06-26 14:26:24.618: V/SmartLyrics(31087): getDetails (SpotifyDownload): Loaded Genius page
06-26 14:26:24.619: I/SmartLyrics(31087): getDetails (SpotifyDownload): Completed getDetails task for /songs/3393276 //1
06-26 14:26:24.850: V/SmartLyrics(31087): getDetails (SpotifyDownload): Loaded Genius page
06-26 14:26:24.852: I/SmartLyrics(31087): getDetails (SpotifyDownload): Completed getDetails task for /songs/3125896 //2 Only two of the three finished.
06-26 14:26:26.199: E/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): getDetails tasks still running
06-26 14:26:31.200: E/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): getDetails tasks still running
06-26 14:26:36.201: E/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): getDetails tasks still running
06-26 14:26:41.202: E/SmartLyrics(31087): getGeniusSearchResults (SpotifyDownload): getDetails tasks still running //It gets stuck in a loop here because the getDetails method never fiishes.
唯一被卡住的歌曲是在“专辑”对象上具有空值的歌曲。这里有帮助吗?
答案 0 :(得分:1)
如果DateId PayAmount Customer
-------------------------------------
2019-06 $300 A
2019-06 $300 B
2019-04 $200 B
2019-03 $300 A
为空,则可能在此行上收到album
,并且某些内容处理得不好:
NullReferenceException
尝试使用album = (string)parsed["response"]["song"]["album"]["name"],
代替方括号链:
SelectToken()