I try to receive my data from my API point, but everytime I receive message:
result Id = 1, Status = WaitingForActivation, Method = {null}
This is my ResService_HQ_AHS class:
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using HydroMeteoAnalyzer.Models.API;
using Newtonsoft.Json;
namespace HydroMeteoAnalyzer.Services
{
public class RestService_HQ_AHS
{
HttpClient _client;
public RestService_HQ_AHS()
{
_client = new HttpClient();
}
public async Task<HQ_AHS> Get_HQ_AHS(string query)
{
HQ_AHS HQ_Data = new HQ_AHS();
try
{
var response = await _client.GetAsync(query);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
HQ_Data = JsonConvert.DeserializeObject<HQ_AHS>(content);
}
}
catch (Exception ex)
{
Debug.WriteLine("\t\tERROR {0}", ex.Message);
}
return HQ_Data;
}
}
}
Here is the object from json:
using System;
namespace HydroMeteoAnalyzer.Models.API
{
public class HQ_AHS
{
public int Station { get; set; }
public string Ime { get; set; }
}
}
Here is my Constants class with API EndPoint:
using System;
namespace HydroMeteoAnalyzer.Models.API
{
public class Constants
{
public static string EndPoint = "http://194.141.118.43/api/stations";
}
}
Here is my page which I try to receive the data from Json file:
using System;
using System.Collections.Generic;
using HydroMeteoAnalyzer.Models.API;
using HydroMeteoAnalyzer.Services;
using Xamarin.Forms;
namespace HydroMeteoAnalyzer.Models.Hydro
{
public partial class HQ_AHS : ContentPage
{
RestService_HQ_AHS _restServiceStations;
public HQ_AHS ()
{
InitializeComponent ();
_restServiceStations = new RestService_HQ_AHS();
CheckStation_HQ_AHS();
}
string GenerateRequestUriStations(string endpoint)
{
string requestUri = endpoint;
requestUri = $"stations";
return requestUri;
}
public void CheckStation_HQ_AHS()
{
var result = _restServiceStations.Get_HQ_AHS(Constants.EndPoint);
var test = result;
}
}
}
Everytime I receive null on my test
and receive message:result Id = 1, Status = WaitingForActivation, Method = {null}
and In the RestService_HQ_AHS
cannot enter in the If statement..
This is the API point: http://194.141.118.43/api/stations
What do I need to change to get my data from the API ?
CodePudding user response:
The problem is that you're not awaiting the response from Get_HQ_AHS
. This needs to be awaited in the CheckStation_HQ_AHS
method. Update the signature to async Task
too (async void
should generally be avoided):
public async Task CheckStation_HQ_AHS()
{
var result = await _restServiceStations.Get_HQ_AHS(Constants.EndPoint);
...
}
Additionally, the json response is a list/array of type List<HQ_AHS>
, so you should try deserializing using:
var content = await response.Content.ReadAsStringAsync();
var responseModel = JsonConvert.DeserializeObject<List<HQ_AHS>>(content);
// deserialize to list here ^^^
You haven't shown HQ_Data
so not sure how the response maps to that class, but you should be able to deserialize using the above code.