如何使用php / MySQL将特定登录用户(带有ID)的数据传递给xamarin仿真器?

问题描述 投票:-2回答:1

我真的需要您的帮助!

大家好,我已经在这个问题上停留了两个星期,我真的找不到答案...

我们已经在Xamarin Forms应用程序上创建了一个登录系统。每次用户登录时,该特定用户的ID都会传递到我们应用中的其他页面,以便我们可以在每次想要的地方和各处使用该ID来访问该用户的信息。

在下面的代码中,将ID传递给它时,您可以看到我们的个人资料页面。我们遇到的问题是,当我们打印u.Username,u.Email,u.Password等的输出时,我们得到的输出为'null':

((((您可以看到我们为每个数据(用户名,电子邮件,密码和年龄)都设置了不同的php脚本,以使我们更容易访问数据)]

readonly HttpClient client = new HttpClient(new HttpClientHandler());
    static readonly user u = new user();

    public async void GetUserData(string Id)             // here we pass the Id of a user when logged in
    {
        HttpResponseMessage res;
        u.Id = Id; // if we print the 'u.Id' we have the correct Id, so the php script works

        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("Id", u.Id)
        });

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/UsernameById.php", content);
        u.Username = await res.Content.ReadAsStringAsync();

        Console.WriteLine("-----------------------------------------");
        Console.WriteLine(u.Username);              // look at output beneath (result = 'null')



        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/EmailById.php", content);
        u.Email = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Email);

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/PasswdById.php", content);
        u.Passwd = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Passwd);

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/AgeById.php", content);
        u.Age = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Age);
    }

1

但是当我们在代码的开头给出ID时,我们将获得正确的输出:

readonly HttpClient client = new HttpClient(new HttpClientHandler());
    static readonly user u = new user();

    public async void GetUserData(string Id)
    {
        HttpResponseMessage res;
        u.Id = "12"; //we give the Id of the user that we want to use data

        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("Id", u.Id)
        });

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/UsernameById.php", content);
        u.Username = await res.Content.ReadAsStringAsync();

        Console.WriteLine("-----------------------------------------");
        Console.WriteLine(u.Username);



        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/EmailById.php", content);
        u.Email = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Email);

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/PasswdById.php", content);
        u.Passwd = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Passwd);

        res = await client.PostAsync("http://10.0.2.2/DATA/USER/DataByID/AgeById.php", content);
        u.Age = await res.Content.ReadAsStringAsync();

        Console.WriteLine(u.Age);
    }

1

authentication xamarin.forms null id
1个回答
0
投票

@@ Jason

这是我的php脚本,通过传递用户的登录ID并将该ID作为Xamarin中的参数传递来获取我的用户名。 (该数据尚不能用于SQL注入保护,我稍后将对其进行修复)

<?php

$db = new mysqli('localhost', 'root', '', 'test');

$Id = mysqli_real_escape_string($db, $_POST['Id']);

$res = mysqli_query($db, "SELECT Username from User WHERE Id = '$Id'");

$row = mysqli_fetch_assoc($res);

echo json_encode($row);

mysqli_close($db);

?>
© www.soinside.com 2019 - 2024. All rights reserved.