i hope that you can help. i was searching through the whole internet to get an answer but i couldn´t find one. i am a beginner in C# and the .NET Framework and i am recently working with an API to fetch some Data about movies and also to learn how to work with APIs in general.
I am using the MVVM Pattern to accomplish my goals.
So, i´ve implemented the httpClient in a Class and i created a model class for the fetched data. Now when i Invoke the Method e.g. in a ViewModel to initialize a new Object of the model class, the IDE tells me that i cannot convert type Tasks.Task "model class" to "model class".
I hope the code example makes it more clear what i am trying to explain:
public class MovieDatabase
{
private static readonly HttpClient httpClient = new HttpClient();
public static async Task<MovieDatabaseResponse> GetDataAsync()
{
try
{
string finalQuery = $@"https://api.themoviedb.org/3/movie/550?api_key={ApiKey}&language=de-DE";
string responseBody = await httpClient.GetStringAsync(finalQuery);
MovieDatabaseResponse? movieDatabaseResponse = JsonConvert.DeserializeObject<MovieDatabaseResponse>(responseBody);
return movieDatabaseResponse;
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
MovieDatabaseResponse? emptyresponse = null;
return emptyresponse;
}
}
}
public class MovieDatabaseResponse
{
public string title;
public string overview;
public string tagline;
}
public MainWindow()
{
InitializeComponent();
MovieDatabaseResponse theResponseObject = MovieDatabase.GetDataAsync();
}
I've made the GetDataAsync() method static just to test it fast in the mainWindow because I didn´t build the pattern/architecture properly yet.
Thanks in advance, guys!
I've tried to put it in a var variable but the debugger told me that it wasn't yet computed.