Reputation: 5
In C # UWP I would like to display the data from the Zomato API via Gridview, but when I run the following code there is no result. In the meantime, when I run the API in the browser, the json code works. Please help me. Thank you!
XAML code here:
<PivotItem Header="Restoranlar">
<Grid SizeChanged="Grid_SizeChanged" Background="{ThemeResource SystemBaseLowColor}">
<GridView x:Name="itemGridView">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Restaurant">
<Grid>
<Image x:Name="menu" Source="{x:Bind photos_url}" HorizontalAlignment="Center" Stretch="Uniform"/>
<StackPanel Margin="0,-25,0,0" Height="25" VerticalAlignment="Bottom">
<TextBlock x:Name="name" Text="{x:Bind name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/>
</StackPanel>
<TextBlock x:Name="ID" Text="{x:Bind url}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="17" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</PivotItem>
C# code here:
private async void GetRestaurants()
{
try
{
string strBingImageURL = string.Format("https://developers.zomato.com/api/v2.1/search?apikey=" + APIKEY +"&count=10&lat=" + LatitudeF + "&lon=" + LongitudeF + "&category=restaurants&sort=real_distance&order=asc");
string jsonText = "";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri(strBingImageURL));
jsonText = await response.Content.ReadAsStringAsync();
JsonArray jsonData1 = JsonArray.Parse(jsonText);
foreach (JsonValue groupValue in jsonData1)
{
if (jsonData1.Count > 0)
{
JsonObject groupObject = groupValue.GetObject();
string zname = groupObject["name"].GetString();
string zphotos = groupObject["photos_url"].GetString();
string zurl = groupObject["url"].GetString();
Restaurant file = new Restaurant();
file.name = zname;
file.url = zurl;
file.photos_url = zphotos;
}
else
{
//DoNothing
}
}
}
catch (HttpRequestException ex)
{
//Catch Here
}
}
Restaurant class here:
public class Restaurant
{
public string name { get; set; }
public string url { get; set; }
public string photos_url { get; set; }
}
Upvotes: 0
Views: 463
Reputation: 531
I assume your GetRestaurants
method is in the code-behind of your View or in whatever you have as the DataContext for the View.
You'll need to add a public property of type List<Restaurant>
or ObservableCollection<Restaurant>
. Like so:
ObservableCollection<Restaurant> Restaurants { get; set; }
Then in your foreach
your new Restaurant
object file
to this List
or ObservableCollection
. I would recommend using an ObservableCollection
.
Finally in your View, all you need to do is add the Restaurants
property as the GridView
's ItemsSource
. Like so:
<GridView x:Name="itemGridView" ItemsSource="{x:Bind Restaurants}">
-Edit-
Where you are also going wrong is with the JSON deserialization. You aren't actually deserializing it in your code. You need to actually deserialize it into the objects it expects a better explanation and example of JSON deserialization can be found in this answer on StackOverflow (see below for a snippet of what I've implemented to make your code work)
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(new Uri(zomatoURL));
jsonText = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<RootObject>(jsonText);
To see a full working sample of the Zomato API and some MVVM techniques see my sample here.
Upvotes: 2