uwp网络访问

作业要求

  • 使用HttpWebRequest或HttpClient访问网络
  • 输入城市查天气,快递查询等生活实用功能至少完成一种
  • 自行上网查找API(不可与Demo相同!)
  • Bonus:完成两种不同的API,且分别为JSON和XML格式

准备工作

选择管理方案的Nuget程序包
Nuget
搜索httpclient,并安装
httpclient

具体实现

先给一个粉红少女系的截图,解析Json得到温度,解析Xml得到PM2.5
运行结果

再给一个文件目录
文件目录

MainPage.xaml

<Page
    x:Class="week7.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:week7"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="HotPink">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <Button x:Name="JsonsearchWeather" Content="Json查询天气"  Click="searchWeather_Click"></Button>
            <TextBox x:Name="JsonlocationInput"  Text="北京"  Height="50" Width="200"></TextBox>
            <TextBox x:Name="JsonweatherResult"  Text="hello weather" Height="50" Width="200"></TextBox>
        </StackPanel>

        <StackPanel Grid.Row="1">
            <Button x:Name="XmlsearchWeather" Content="Xml查询天气"  Click="XmlsearchWeather_Click"></Button>
            <TextBox x:Name="XmllocationInput"  Text="北京"  Height="50" Width="200"></TextBox>
            <TextBox x:Name="XmlweatherResult"  Text="hello weather" Height="50" Width="200"></TextBox>
        </StackPanel>
    </Grid>
</Page>

MainPage.xmal.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net;
using System.Net.Http;
using week7;
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板

namespace week7
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void searchWeather_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RootObject myWeather = await OpenWeatherMapProxy.GetWeather(JsonlocationInput.Text);

                JsonweatherResult.Text = myWeather.results[0].location.name + "的温度是" + myWeather.results[0].now.temperature;
            }
            catch
            {

            }
        }

        private async void XmlsearchWeather_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var XmlmyWeather = await xmlway.GetWe
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值