Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms - How to get the json string in POST method response?

I created a listview where it displays all categories based on the data from an API "cat_code", and if you tapped any of the it will transfer the value of the "cat_code" into a variable "selectedItem"

MenuCategories.xaml

<ListView x:Name="MyCategory" ItemSelected="MyCategory_ItemSelected" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell >
                <StackLayout Orientation="Horizontal" HorizontalOptions="Center" VerticalOptions="Center"   >
                    <Label Font="30" HorizontalTextAlignment="Center" x:Name="categoryname" Text="{Binding cat_code}" 
                       Style="{DynamicResource ListItemTextStyle}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

MenuCategories.xaml.cs

private string selectedItem;

public MenuCategories()
{
    InitializeComponent();
    GetCategoryAsync();
}

public async Task GetCategoryAsync()
{
    HttpClient client = new HttpClient();
    var response = await client.GetStringAsync("http://ropenrom24-001-site1.etempurl.com/potangina/final/Restserver/index.php/category/view");
    var cat = JsonConvert.DeserializeObject<List<Catergory>>(response);
    MyCategory.ItemsSource = cat;
}

private void MyCategory_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    var selectedCategory = e.SelectedItem as Catergory;
    if (selectedCategory != null)
        selectedItem = selectedCategory.cat_code;
    DisplayAlert("Test", "Selected: " + selectedItem, "OK");

    Catergory cat = new Catergory();
    {
        cat.cat_code = selectedItem;
    }

    var json = JsonConvert.SerializeObject(cat);

    var content = new StringContent(json, Encoding.UTF8, "application/json");

    HttpClient client = new HttpClient();

    var result = await client.PostAsync("http://ropenrom24-001-site1.etempurl.com/potangina/final/Restserver/index.php/Products/view_cat", content);
}

If I post a cat_code:Asian it will display all of cat_code that has cat_code:Asian what I want to happen is how to get the underlined in this picture? picture

and transfer it to a viewmodel where i can display it to this listview?

MenuView.xaml

<ListView x:Name="ViewMenu">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <Image Source="{Binding menu_image ,StringFormat='https://i.imgur.com/{0:F0}.png'}" Scale="1" />
                    <Label Text="{Binding menu_name}" Font="30"/>
                    <Label Text="{Binding menu_price,StringFormat=''}"/>
                    <Label Text="{Binding menu_availability} "/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Menus.cs

public class Menus
{
    public string menu_code { get; set; }
    public string cat_code { get; set; }
    public string menu_name { get; set; }
    public string menu_price { get; set; }
    public string menu_description { get; set; }
    public string menu_image { get; set; }
    public string menu_inventory { get; set; }
    public string menu_availability { get; set; }
}

Basically my question is how to get the json string underlined in the picture above?

like image 574
AgentRed Avatar asked Dec 17 '25 19:12

AgentRed


1 Answers

try this.

string json_response = await result.Content.ReadAsStringAsync();

the json_response should contain the data you need.

like image 147
ropenrom24 Avatar answered Dec 19 '25 08:12

ropenrom24



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!