我在Xamarin中拥有一个跨平台应用程序,目前在Android上运行良好,但是,我现在正在Windows计算机上使用iPhoneSimulator在iOS上调试它(我在Cloud Service中已与MAC连接)。
我在Web服务中进行查询的部分的控制器上放置了一个停止点,并且确实看到我的Username和Password参数正在填充视图(MainPage或Logging Page)中的信息。
我当前的问题是,当我单击MainPage(即登录页面)上的访问按钮时,它什么也没做,只是停留在那儿。
此刻是主页XAML代码:
<Image Source="taber.png"
FlexLayout.Order="-1"
FlexLayout.AlignSelf="Center"
WidthRequest="180"
HeightRequest="180" Aspect="AspectFit" />
<Label>
</Label>
<Frame
CornerRadius="15"
Padding="0"
BackgroundColor="#507BF5"
Margin="15,10,15,10"
HasShadow="False"
>
<Label
Margin="5"
Text="Usuario"
HorizontalOptions="Center"
BackgroundColor="Transparent"
FontSize="Large"
TextColor="White"
FontAttributes="Bold"
/>
</Frame>
<Frame
CornerRadius="15"
Padding="0"
Margin="15,10,15,10"
>
<Entry Placeholder="Usuario"
x:Name="txtUsuario"
HorizontalTextAlignment="Center"
TextChanged="OnChangeUser"
/>
</Frame>
<Frame
CornerRadius="15"
Padding="0"
BackgroundColor="#507BF5"
Margin="15,10,15,10"
HasShadow="False"
>
<Label
Margin="5"
Text="Contraseña"
HorizontalOptions="Center"
BackgroundColor="Transparent"
FontSize="Large"
TextColor="White"
FontAttributes="Bold"
/>
</Frame>
<Frame
CornerRadius="15"
Padding="0"
Margin="15,10,15,10"
>
<Entry IsPassword="True"
Placeholder="Contraseña"
x:Name="txtPassword"
HorizontalTextAlignment="Center"
/>
</Frame>
<Label FlexLayout.Grow="1" />
<Button
BorderColor="#507BF5"
BorderRadius="5"
BorderWidth="2"
Margin="15,10,15,10"
x:Name="bntIngresar"
Text="Ingresar"
TextColor="White"
Clicked="btnIngresar_click"
BackgroundColor="#507BF5"/>
我使用btnIngresar_click方法通过PHP Web服务验证信息,这是XAML.CS中用于btnIngresar_click:
的代码。private async void btnIngresar_click(object sender, EventArgs e)
{
try
{
string input = txtPassword.Text;
string encryptedPass;
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
encryptedPass = sb.ToString();
UseManager manager = new UseManager();
var result = await manager.userLogin(txtUsuario.Text.ToString(), encryptedPass);
if (result.Count() > 0)
{
foreach (var nuser in result)
{
servidor = nuser.id_servidor;
acceso = nuser.id_acceso;
usuario = nuser.id;
}
//await Navigation.PushAsync(new Principal(servidor, acceso, usuario));
await Navigation.PushAsync(new SplashPage(servidor, acceso, usuario));
}
else
{
await DisplayAlert("Error", "Usuario o Contraseña Incorrectos", "Accept", "Cancel");
}
}
catch (Exception e1)
{
}
}
现在是方法userLogin,该方法将我带到PHP中的Web服务:(上面提到的FYI在行中放置了一个停止点:var result = await cliente.GetAsync (URL +“ login.php?username =” +用户名+“&pass =” =“ +密码);,我可以看到正在捕获用户名变量,并且也捕获了密码。)
public async Task<IEnumerable<user>> userLogin(string username, string password)
{
HttpClient cliente = getClient();
var result = await cliente.GetAsync(URL + "login.php?username=" + username + "&password=" + password);
if (result.IsSuccessStatusCode)
{
var content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<user>>(content);
}
return Enumerable.Empty<user>();
}
最后是PHP中的Web服务:
<?php
$username= $_REQUEST['username'];
$password= $_REQUEST['password'];
$cnx=new PDO("mysql: host=localhost; dbname=securitb","DBUSERNAME","DBPASSWORD");
$res=$cnx->query("select * from usuarios where username ='$username' and password ='$password'");
$datos = array();
foreach ($res as $row){
array_push($datos, array(
'id' => $row['id'],
'username' => $row['username'],
'password' => $row['password'],
'activo' => $row['activo'],
'id_acceso' => $row['id_acceso'],
'id_servidor' => $row['id_servidor'],
));
}
echo utf8_encode(json_encode($datos));
?>
为什么可能是我的iOS应用未移至启动画面?
第一个问题是您使用自己的代码提出了多个请求:
private HttpClient getClient()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json"); client.DefaultRequestHeaders.Add("Connection", "close");
return client;
}
我个人认为主要问题在这里:
try
{
HttpClient cliente = getClient();
var result = await cliente.GetAsync(URL + "login.php?username=" + username + "&password=" + password);
if (result.IsSuccessStatusCode)
{
var content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IEnumerable<user>>(content);
}
return Enumerable.Empty<user>();
}
catch (Exception)
{
}
尝试在此处捕获例外。我认为对象反序列化存在问题。