How to get Json's return from the Hgbrasil Weather site in my Xamarin application

1

How do I get the values available in 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; }
}
    
asked by anonymous 31.01.2018 / 20:58

1 answer

0

There are no problems with the above code, but the problem is with the api link that there must be a problem, which I do not know Come on.

I would love the link to work because I would not 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 sites

Weather forecast: link

News link

    
31.01.2018 / 22:44