Xamarin System.NullReferenceException:当我尝试使用 Api Rest 时出现 'Object reference not set to an instance of an object.'
Xamarin System.NullReferenceException: 'Object reference not set to an instance of an object.' appears when I try to use Api Rest
我不明白为什么当我尝试登录时,程序 returns 一个 System.NullReferenceException,当我执行检查以防止输入的值为 null
这是模型:
using Newtonsoft.Json;
namespace StockingApp.Models
{
public class LoginModel
{
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
}
这是我用来登录的代码:
private async void loginButton_Clicked(object sender, EventArgs e)
{
LoginModel log = new LoginModel
{
Email = txtEmail.Text,
Password = txtPassword.Text
};
if (txtEmail.Text != null | txtPassword.Text != null)
{
Uri RequestUri = new Uri("The url of the Api");
var client = new HttpClient();
var json = JsonConvert.SerializeObject(log);
var contentJson = new StringContent(json, Encoding.UTF8, "application/json");
//this is the line that causes the exception
var response = await client.PostAsync(RequestUri, contentJson);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Iniciar Sesión", "El inicio de sesión se realizó correctamente", "Ok");
}
else
{
await DisplayAlert("Error", "Los datos introducidos son incorrectos", "Ok");
}
}
else
{
await DisplayAlert("Error", "Faltan datos por introducir", "Ok");
}
}
这是导致异常的行,我不明白为什么响应为空
var response = await client.PostAsync(RequestUri, contentJson);
这是 Xaml 以及 Xaml 的外观:How it looks
<ContentPage.Content>
<StackLayout Spacing="5" Padding="10" VerticalOptions="Center">
<Entry x:Name="txtEmail" Placeholder="{translator:Translate prompt_email}" Style="{StaticResource entryStyles}"/>
<Entry x:Name="txtPassword" Placeholder="{translator:Translate prompt_password}" Style="{StaticResource entryStyles}" IsPassword="True"/>
<Button x:Name="loginButton" Text="{translator:Translate title_activity_login}" Style="{StaticResource buttonStyles}" Clicked="loginButton_Clicked"/>
<Label Text="{translator:Translate tit_contacto}" HorizontalTextAlignment="Center"/>
<Label x:Name="link" Margin="20" HorizontalOptions="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="www.atecresa.com" TextColor="GreenYellow" TextDecorations="Underline">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}"
CommandParameter="https://atecresa.com" NumberOfTapsRequired="1"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</ContentPage.Content>
这是招摇登录:Swagger of the api
我检查了 Postman,api 工作正常:Postman check
也许试试这个
if (txtEmail.Text != null && txtPassword.Text != null)
我有类似的错误,我只是将 url 的 http 更改为 https。我认为这就是问题所在。
我不明白为什么当我尝试登录时,程序 returns 一个 System.NullReferenceException,当我执行检查以防止输入的值为 null
这是模型:
using Newtonsoft.Json;
namespace StockingApp.Models
{
public class LoginModel
{
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("password")]
public string Password { get; set; }
}
}
这是我用来登录的代码:
private async void loginButton_Clicked(object sender, EventArgs e)
{
LoginModel log = new LoginModel
{
Email = txtEmail.Text,
Password = txtPassword.Text
};
if (txtEmail.Text != null | txtPassword.Text != null)
{
Uri RequestUri = new Uri("The url of the Api");
var client = new HttpClient();
var json = JsonConvert.SerializeObject(log);
var contentJson = new StringContent(json, Encoding.UTF8, "application/json");
//this is the line that causes the exception
var response = await client.PostAsync(RequestUri, contentJson);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Iniciar Sesión", "El inicio de sesión se realizó correctamente", "Ok");
}
else
{
await DisplayAlert("Error", "Los datos introducidos son incorrectos", "Ok");
}
}
else
{
await DisplayAlert("Error", "Faltan datos por introducir", "Ok");
}
}
这是导致异常的行,我不明白为什么响应为空
var response = await client.PostAsync(RequestUri, contentJson);
这是 Xaml 以及 Xaml 的外观:How it looks
<ContentPage.Content>
<StackLayout Spacing="5" Padding="10" VerticalOptions="Center">
<Entry x:Name="txtEmail" Placeholder="{translator:Translate prompt_email}" Style="{StaticResource entryStyles}"/>
<Entry x:Name="txtPassword" Placeholder="{translator:Translate prompt_password}" Style="{StaticResource entryStyles}" IsPassword="True"/>
<Button x:Name="loginButton" Text="{translator:Translate title_activity_login}" Style="{StaticResource buttonStyles}" Clicked="loginButton_Clicked"/>
<Label Text="{translator:Translate tit_contacto}" HorizontalTextAlignment="Center"/>
<Label x:Name="link" Margin="20" HorizontalOptions="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="www.atecresa.com" TextColor="GreenYellow" TextDecorations="Underline">
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}"
CommandParameter="https://atecresa.com" NumberOfTapsRequired="1"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</ContentPage.Content>
这是招摇登录:Swagger of the api
我检查了 Postman,api 工作正常:Postman check
也许试试这个
if (txtEmail.Text != null && txtPassword.Text != null)
我有类似的错误,我只是将 url 的 http 更改为 https。我认为这就是问题所在。