0

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, now as a result I see that the objects are coming but "Station" property come 0 "Ime" property come null. Another thing I notice is that in the file RestService_HQ_AHS Unable to enter the if statement..
@BenJohnson That explains why you're getting default values in the response - if it's not going in the if statement then the request doesn't have a 200 success code so something went wrong. Try to debug the issue. What is the actual response code? 400 bad request, 500 server error...?
ex {Java.IO.IOException: Cleartext HTTP traffic to 194.141.118.43 not permitted at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00…}
That's an error coming from Android/Java. I'm not sure if this will resolve the issue but you can try adding this to the Android manifest: android:usesCleartextTraffic="true". See this post for more options. Maybe try using https instead? @BenJohnson
With this code android:usesCleartextTraffic="true" I see the data in response in RestService_HQ_AHS, but I can't see data in var result = await _restServiceStations.Get_HQ_AHS(Constants.EndPoint); I still see the "Station" property is 0 and "Ime" property is null..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.