How to get back Json from the site Hgbrasil Weather in my application Xamarin

Asked

Viewed 216 times

1

How do I get the values provided in the Json format in my Xamarin application.

The code below is not returning anything.

public class MainActivity : Activity
{
    ProgressDialog dialog;
    TextView txtv1;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        txtv1 = FindViewById<TextView>(Resource.Id.txtResposta);

        OlaRestAsync();//Chamada
    }

    private async Task OlaRestAsync()
    {
        var client = new HttpClient();

        var json = await client.GetStringAsync($"https://api.hgbrasil.com/weather/?format=json&city_name=Ribeir%C3%A3o%20Preto&key=2857c531");
        var dados = JsonConvert.DeserializeObject<HgWeather>(json);

        txtv1.Text = dados.results.description;
    }
}

Classes

public class HgWeather
{
    public string by { get; set; }
    public bool valid_key { get; set; }
    public Results results { get; set; }
    public float execution_time { get; set; }
    public bool from_cache { get; set; }
}

public class Results
{
    public int temp { get; set; }
    public string date { get; set; }
    public string time { get; set; }
    public string condition_code { get; set; }
    public string description { get; set; }
    public string currently { get; set; }
    public string cid { get; set; }
    public string city { get; set; }
    public string img_id { get; set; }
    public string humidity { get; set; }
    public string wind_speedy { get; set; }
    public string sunrise { get; set; }
    public string sunset { get; set; }
    public string condition_slug { get; set; }
    public string city_name { get; set; }
    public Forecast[] forecast { get; set; }
}

public class Forecast
{
    public string date { get; set; }
    public string weekday { get; set; }
    public string max { get; set; }
    public string min { get; set; }
    public string description { get; set; }
    public string condition { get; set; }
}

1 answer

0

There are no problems with the above code, but the problem is with the site api https://hgbrasil.com I don’t know what it is.

I would very much like the https://hgbrasil.com work because I wouldn’t worry about translations. But do what.

Trying with another url the code worked perfectly.

var json = await client.GetStringAsync($"https://viacep.com.br/ws/29400000/json/");

It worked perfectly also with the websites

Meteorological: http://openweathermap.org/

News https://newsapi.org/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.