如何选择数据并将其放在来自 Xamarin 中的 HTTP 请求的标签文本上

问题描述 投票:0回答:1

我是 Xamarin 的初学者。我有一个 PHP 页面,可以为一个用户获取 JSON 数据:

<?php
include_once 'DbConnect.php';
$username = $_GET['username'];
$username = strip_tags($username);
$username = trim($username);
$username = stripslashes($username);
$username = mysqli_real_escape_string($connection, $username);
 
$user = "SELECT * FROM users Where username = '$username'";
$result = mysqli_query($connection, $user);
$user_status = array();
foreach ($result as $row) {
    array_push($user_status, array(
        's_id' => $row['s_id'],
        'm_id' => $row['m_id'],
        'd_id' => $row['d_id'],
    ));
}
echo utf8_encode(json_encode($user_status));
?>

在 Xamarin 的一个页面上,我请求此页面获取上面的数据:

private async void GetStatus()
{
    HttpClient MyStatus = new HttpClient();
    var responsestatus = await MyStatus.GetStringAsync("xxx/api/userstatus.php?username=" + myparam.Text);
    var myStatus = JsonConvert.DeserializeObject<List<Models.UserStatus>>(responsestatus);

    var Result = from MyItem in myStatus
    select MyItem;
}

所以我有这个类模型:

class UserStatus
{
    public string s_id { get; set; }
    public string m_id { get; set; }
    public string d_id { get; set; }
}

现在我收到一行( s_id ,m_id 和 d_id )。我想将它们中的每一个都放在标签文本上以显示它们。我怎样才能做到这一点?

json xamarin request
1个回答
2
投票

创建一个包含三个

Labels
的 XAML 页面并给它们命名

<ContentPage ... >
  <ContentPage.Content>
    <StackLayout>
     <Label x:Name="Label1" />
     <Label x:Name="Label2" />
     <Label x:Name="Label2" />
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

然后在您的 xaml.cs 页面中,收到数据后

HttpClient MyStatus = new HttpClient();
var responsestatus = await MyStatus.GetStringAsync("xxx/api/userstatus.php?username="+ myparam.Text);
var myStatus = JsonConvert.DeserializeObject<List<Models.UserStatus>>(responsestatus);

Label1.Text = myStatus[0].s_id;
Label1.Text = myStatus[0].m_id;
Label1.Text = myStatus[0].d_id;
© www.soinside.com 2019 - 2024. All rights reserved.