I am currently following along with Naweed Akram's .NET MAUI tutorial creating a YouTube clone. I'm developing on a Windows machine and testing using an Android emulator. I'm running Visual Studio 2022 v17.3.3. I've run into an issue where the application will timeout when calling the YouTube API. But, and this is the strange part, it only happens in the Android emulator. I've tried testing with the Windows machine option and I am able to make the call successfully. I've even tried testing on my personal Android device (Samsung Note 10+) and am also able to make the call successfully. I've also tried using Postman, and am able to make the call successfully.
Here are the relevant code snippets:
private async Task<string> GetJsonAsync(string resource, int cacheDuration = 24)
{
var cleanCacheKey = resource.CleanCacheKey();
if (_cacheBarrel is not null)
{
var cachedData = _cacheBarrel.Get<string>(cleanCacheKey);
if (cacheDuration > 0 && cachedData is not null && !_cacheBarrel.IsExpired(cleanCacheKey))
{
return cachedData;
}
if (_connectivity.NetworkAccess != NetworkAccess.Internet)
{
return cachedData is not null ? cachedData : throw new InternetConnectionException();
}
}
if (_connectivity.NetworkAccess != NetworkAccess.Internet)
{
throw new InternetConnectionException();
}
var response = await _httpClient.GetAsync(new Uri(_httpClient.BaseAddress, resource));
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
if (cacheDuration > 0 && _cacheBarrel is not null)
{
try
{
_cacheBarrel.Add(cleanCacheKey, json, TimeSpan.FromHours(cacheDuration));
}
catch
{
}
}
return json;
}
The application is getting stuck at the line:
var response = await _httpClient.GetAsync(new Uri(_httpClient.BaseAddress, resource));
This is being called from:
public async Task<VideoSearchResult> SearchVideos(string searchQuery, string nextPageToken = "")
{
var resouceUri = $"search?part=snippet&maxResults=20&type=video&key={Constants.ApiKey}&q={WebUtility.UrlEncode(searchQuery)}" +
(!string.IsNullOrWhiteSpace(nextPageToken) ? $"pageToken={nextPageToken}" : "");
var result = await GetAsync<VideoSearchResult>(resouceUri, 4);
return result;
}
and
public async Task<T> GetAsync<T>(string resource, int cacheDuration = 24)
{
// Get Json data
var json = await GetJsonAsync(resource, cacheDuration);
return JsonSerializer.Deserialize<T>(json);
}
I have no idea what could be causing this. The call works on Windows and even on an actual Android device, just not the emulator.
Any help would be appreciated.
Thanks