微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在发送链接请求之前检查链接请求是否有效? (Xamarin.Forms)

如何解决如何在发送链接请求之前检查链接请求是否有效? (Xamarin.Forms)

我正在做一个简单的测试项目,如果您在条目中输入任何股票代码,它会生成一个 WebClient 和返回该股票价格字符串的链接

我知道我需要从中获取数据的确切 URL,如果用户输入现有的股票代码,它就可以正常工作。但是,如果用户创建股票代码,我会遇到运行时错误,因为程序试图从不存在的网页中提取数据。

在应用程序运行时,它直接进入中断模式并返回错误 System.NullReferenceException: 'Object reference not set to an instance of an object.'

如何在发出请求之前检查 URL 是否有效,或者至少能够在将应用程序置于中断模式之前捕获错误

这是c#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Stock_WatchList
{
    [XamlCompilation(XamlCompilationoptions.Compile)]
    public partial class sybmolSelector : ContentPage
    {
        public string price { get; set; }
        public sybmolSelector()
        {
            InitializeComponent();

            BindingContext = this;
        }

        private void Entry_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e)
        {
            var nameValue = entry.Text.ToString();
            var link = "https://sandBox.iexapis.com/stable/stock/" + nameValue + "/price?token=Tsk_57bebc1d051340dbaad8656ab0027e90";

            var client1 = new WebClient();
            string b = client1.DownloadString(link);

            price = "$" + b;

            Symbol.Text = nameValue.ToString();
            Price.Text = price;
        }
    }
}

和 Xamarin 代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Stock_WatchList.sybmolSelector"
             BackgroundColor="Black"
             NavigationPage.IconColor="HotPink">
    <ContentPage.Content>
        <StackLayout>
            <Label TextColor="HotPink" BackgroundColor="Black" Text="------------------- Enter A Symbol Below -------------------" FontSize="19" LineBreakMode="TailTruncation"></Label>
            <Entry PlaceholderColor="HotPink" PropertyChanged="Entry_PropertyChanged" FontSize="19" x:Name="entry" TextColor="HotPink" BackgroundColor="#111111"></Entry>
            <Label x:Name="Symbol" Margin="5" Text="" HorizontalOptions="Start" TextColor="HotPink" FontSize="40"/>
            <Label x:Name="Price" Margin="5,5,5" Text="" HorizontalOptions="Start" TextColor="HotPink" FontSize="50"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

解决方法

如果 Entry.Text 为 null 或为空,请勿拨打电话。当您的表单加载时,PropertyChanged 将被调用。那时 Text 仍然为空。它当然在你的降价中没有默认值。

private void Entry_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e)
{
    // guard for null
    if (entry == null) return;
    // no need to call the api if we don't have a value
    if (String.IsNullOrEmpty(entry.Text)) return;

    var link = "https://sandbox.iexapis.com/stable/stock/" + entry.Text + "/price?token=sometoken";

    using(var client1 = new WebClient()) // WebClient does implement (useless) IDisposable
    {
         price = "$" + client1.DownloadString(link);
    }

    // maybe these are null as well,who knows
    if (Symbol != null)  Symbol.Text = entry.Text;
    if (Price != null)   Price.Text = price;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。