I want to get data from API from https://rapidapi.com/coinlore/api/coinlore-cryptocurrency/
The result look like:
{2 items
"data":[...]100 items
"info":{...}2 items
}
When I see it this way, I'm not sure how to create objects.
I want to get data array and I create a objects like this:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace SmartCryptoWorld.Models
{
public class Exchange
{
[JsonProperty("data")]
public List<ExchangeBody> CryptoExchange { get; set; }
}
public class ExchangeBody
{
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("price_usd")]
public double Price { get; set; }
[JsonProperty("percent_change_24h")]
public double Percent_Change_24h { get; set; }
[JsonProperty("percent_change_1h")]
public double Percent_Change_1h { get; set; }
[JsonProperty("percent_change_7d")]
public double Percent_Change_7d { get; set; }
[JsonProperty("market_cap_usd")]
public double Market_Cap_USD { get; set; }
}
}
This is the method who works but the data not come in the List and go to catch exception:
private async Task GetExchange()
{
try
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://coinlore-cryptocurrency.p.rapidapi.com/api/tickers/?start=0&limit=100"),
Headers =
{
{ "x-rapidapi-host", "coinlore-cryptocurrency.p.rapidapi.com" },
{ "x-rapidapi-key", "51569aba99mshf9e839fcfce791bp16c0dbjsn9ced6dba7472" },
},
};
using (var response = await client.SendAsync(request))
{
var exchange = new Exchange();
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
var exchangeBody = JsonConvert.DeserializeObject<List<ExchangeBody>>(body);
exchange.CryptoExchange = exchangeBody;
}
}
catch (Exception ex)
{
await DisplayAlert("Alert", "Please, check your internet connection.", "OK");
}
}
In var body = await response.Content.ReadAsStringAsync();
I see the data from API, when I step over with debugger to next line var exchangeBody = JsonConvert.DeserializeObject<List<ExchangeBody>>(body);
I see the catch exception..
So I'm 100% sure that the objects are not as they should be ?
The exception message is:
ex {Java.Net.UnknownHostException: Unable to resolve host "coinlore-cryptocurrency.p.rapidapi.com": No address associated with hostname ---> Java.Lang.RuntimeException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname) --- End of inne…}