WP8.1小梦词典开发1:金山词霸API使用

原文出自:http://www.bcmeng.com/windows-phone-api/

今天开始小梦给大家分享一下小梦词典开发中几个关键问题,首先我们来看查词功能的实现.小梦词典的查词功能是通过金山词霸的查词API来实现的.首先我们需要申请金山词霸API的key:

金山词霸API的key申请:

申请地址:http://open.iciba.com/?c=api#jhjy   进入后输入您的网站名,网站地址和你的邮箱就可以,key会发送到你的邮箱里.我试过了,网站名和网站地址可以随意填写,只要邮箱是你的就可以.

金山词霸查词API数据返回格式:

<?xml version="1.0" encoding="UTF-8"?>

-<dict name="219" id="219" num="219">

<key>love</key>

<ps>l?v</ps>

<pron>http://res.iciba.com/resource/amp3/oxford/0/4f/5b/4f5bbc0f19c33e5f1a0b6b974b4eacce.mp3</pron>

<ps>l?v</ps>

<pron>http://res.iciba.com/resource/amp3/1/0/b5/c0/b5c0b187fe309af0f4d35982fd961d7e.mp3</pron>

<pos>vt.& vi.</pos>

<acceptation>爱,热爱;爱戴;喜欢;赞美,称赞; </acceptation>

<pos>vt.</pos>

<acceptation>喜爱;喜好;喜欢;爱慕; </acceptation>

<pos>n.</pos>

<acceptation>爱情,爱意;疼爱;热爱;爱人,所爱之物; </acceptation>

-<sent>

<orig> They happily reflect the desire for a fusional love that inspired the legendary LOVE bracelet Cartier. </orig>

<trans> 快乐地反映出为富有传奇色彩的卡地亚LOVE手镯所赋予的水乳交融之爱恋情愫. </trans>

</sent>

-<sent>

<orig> Love is the radical of lovely , loveliness , and loving. </orig>

<trans> Love是lovely, loveliness 及loving的词根. </trans>

</sent>

-<sent>

<orig> She rhymes " love " with " dove ". </orig>

<trans> 她将 " love " 与 " dove " 两字押韵. </trans>

</sent>

-<sent>

<orig> In sports, love means nil. </orig>

<trans> 体育中, love的意思是零. </trans>

</sent>

-<sent>

<orig> Ludde Omholt with his son, Love, in S ? derma a bohemian and culturally rich district in Stockholm. </orig>

<trans> LuddeOmholt和他的儿子Love在南城 —— 斯德哥尔摩市 的一个充满波西米亚风情的文化富饶区散步. </trans>

</sent>

</dict>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Networking.Connectivity;
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;
namespace jinshanAPI
{

    public sealed partial class MainPage : Page
    {
         string keyWord = null;
        HttpClient httpClient = null;
        public static string loadKey = "1F9CA812CB18FFDFC95FC17E9C57A5E1";
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
            httpClient = new HttpClient();
            httpClient.MaxResponseContentBufferSize = 256000;//缓冲的最大字节数
            httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");//发送的标题
        }

        private void txtKeywords_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter)
            {
                Search();
            }
        }

        async void Search()
        {
            bool isOnline = CheckNetwork();
            if (isOnline)
            {
                string temp = txtKeywords.Text.Trim();
                if (temp != keyWord && !string.IsNullOrEmpty(temp))
                {
                    keyWord = temp;
                    await SearchWordFromAPI(keyWord);
                }
            }

        }

        private async Task<bool> SearchWordFromAPI(string keyWord)
        {
            bool haveResult = false;
            string url = "http://dict-co.iciba.com/api/dictionary.php?w=" + keyWord + "&key=" + loadKey;
            XDocument xResult = null;
            try
            {
                HttpResponseMessage response = await httpClient.GetAsync(url);
                Stream responseBodyAsStream = await response.Content.ReadAsStreamAsync();
                xResult = XDocument.Load(responseBodyAsStream);
                XElement dict = null;
                if (xResult != null)
                {
                    dict = xResult.Root;
                }
                if (dict.Elements().Count() <= 1)
                {
                    txtMag.Visibility = Visibility.Visible;
                    spResult.Visibility = Visibility.Collapsed;
                    txtMag.Text = "亲:对不起!没有找到" + keyWord + "的相关词典解释";
                }
                else
                {
                    haveResult = true;
                    txtMag.Visibility = Visibility.Collapsed;
                    spResult.Visibility = Visibility.Visible;

                    IEnumerable<XElement> pss = dict.Elements(XName.Get("ps"));
                    if (pss.Count() == 2)
                    {
                        spPron.Visibility = Visibility.Visible;
                        List<XElement> psList = pss.ToList();
                        txtPsUK.Text = "英:" + "[" + psList[0].Value + "]";
                        txtPsUs.Text = "美:" + "[" + psList[1].Value + "]";
                    }
                    else if (pss.Count() == 1)
                    {
                        spPron.Visibility = Visibility.Visible;
                        XElement ps = pss.FirstOrDefault();
                        txtPsUK.Text = "[" + ps.Value + "]";
                        txtPsUs.Text = string.Empty;
                    }
                    else
                    {
                        txtPsUK.Text = string.Empty;
                        txtPsUs.Text = string.Empty;
                        spPron.Visibility = Visibility.Collapsed;
                    }

                    IEnumerable<XElement> prons = dict.Elements(XName.Get("pron"));
                    if (prons.Count() == 2)
                    {
                        List<XElement> pronlist = prons.ToList();
                        mePronUK.Source = new Uri(pronlist[0].Value);
                        mePronUs.Source = new Uri(pronlist[1].Value);
                        btnPronUK.Visibility = Visibility.Visible;
                        btnPronUs.Visibility = Visibility.Visible;
                    }
                    else if (prons.Count() == 1)
                    {
                        XElement pron = prons.FirstOrDefault();
                        mePronUK.Source = new Uri(pron.Value);
                        btnPronUK.Visibility = Visibility.Visible;
                        btnPronUs.Visibility = Visibility.Collapsed;
                    }
                    else
                    {
                        btnPronUK.Visibility = Visibility.Collapsed;
                        btnPronUs.Visibility = Visibility.Collapsed;
                    }
                    IEnumerable<XElement> poss = dict.Elements(XName.Get("pos"));
                    List<string> posList = new List<string>();
                    if (poss.Count() > 0)
                    {
                        foreach (XElement pos in poss)
                        {
                            posList.Add(pos.Value);
                        }
                    }

                    IEnumerable<XElement> acceptations = dict.Elements(XName.Get("acceptation"));
                    spAcceptions.Children.Clear();
                    if (acceptations.Count() > 0)
                    {
                        int i = 0;
                        foreach (XElement acceptation in acceptations)
                        {
                            TextBlock textAcceptation = new TextBlock();
                            textAcceptation.FontSize = 20;
                            textAcceptation.TextWrapping = TextWrapping.Wrap;
                            textAcceptation.Margin = new Thickness(0);
                            textAcceptation.Text = posList[i] + acceptation.Value;
                            i++;
                            spAcceptions.Children.Add(textAcceptation);
                        }
                    }

                    IEnumerable<XElement> sents = dict.Elements(XName.Get("sent"));
                    spSends.Children.Clear();
                    if (sents.Count() > 0)
                    {
                        foreach (XElement sent in sents)
                        {
                            XElement orig = sent.Element(XName.Get("orig"));
                            TextBlock textOrig = new TextBlock();
                            textOrig.FontSize = 20;
                            textOrig.TextWrapping = TextWrapping.Wrap;
                            textOrig.Text = orig.Value;
                            spSends.Children.Add(textOrig);
                            XElement trans = sent.Element(XName.Get("trans"));
                            TextBlock textTrans = new TextBlock();
                            textTrans.FontSize = 20;
                            textTrans.TextWrapping = TextWrapping.Wrap;
                            textTrans.Text = trans.Value;
                            spSends.Children.Add(textTrans);
                        }
                    }

                }
            }
            catch
            {
                txtMag.Visibility = Visibility.Visible;
                spResult.Visibility = Visibility.Collapsed;
                txtMag.Text = "亲:网络访问失败!";
            }

            return haveResult;
        }

        bool CheckNetwork()
        {
            bool isOnline = false;
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            if (InternetConnectionProfile == null)
            {
                txtMag.Visibility = Visibility.Visible;
                spResult.Visibility = Visibility.Collapsed;
                txtMag.Text = "亲:断网情况下无法显示词典内容!";

            }
            else
            {
                isOnline = true;
            }
            return isOnline;
        }

        private void btnPronUK_Click(object sender, RoutedEventArgs e)
        {
            mePronUK.Play();
        }

        private void txtPronUs_Click(object sender, RoutedEventArgs e)
        {
            mePronUs.Play();
        }
    }
}

源代码下载:

点我下载!

时间: 2024-07-30 04:38:20

WP8.1小梦词典开发1:金山词霸API使用的相关文章

WP8.1小梦词典开发2:百度翻译API使用

原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天一样首先我们需要申请百度翻译API的Key: 百度翻译API的Key的申请: 进入 http://developer.baidu.com/ 需要一个百度账号,注册登陆后. 点击右上方的  管理服务台 ,选择开发者服务管理,进入口,选择创建工程就可以.创建工程完成后,你就可以拿到你的Key.和金山词霸

小梦词典WP8.1应用发布

这几天一直在做这款应用,今天终于发布了! 小梦词典简介: 小梦词典是一款永久免费无广告的网络词典. 支持英汉单词查询: 支持中,英,法,韩,德,俄,日七国语言翻译,多语言极致体验: 支持生词本记忆,查询,播放: 支持OneDrive云端上传,下载,更新生词: 支持动态磁铁显示最新生词和当前生词总数,让您及时记忆生词: 全面支持单词和翻译结果语音. 小梦词典会持续更新,不断丰富功能! 小梦词典手机下载地址: http://www.windowsphone.com/s?appid=4b3e5848-

小程序&#183;云开发的HTTP API调用丨实战

小程序云开发之httpApi调用. 小程序云开发之httpApi调用(返回"47001处理") 技术栈 采用 nodejs + express 搭建web服务器,采用 axios 请求第三方 httpApi nodejs express axios 项目结构 通过应用生成器工具 express-generator 可以快速创建一个应用的骨架. 主要的核心文件 routes/base.js(api设置),util/rq.js(axios封装),views/base.pug(接口文档) |

小梦接触编程俩年的感悟

(终于把windows phone版的 2048 搞定了,好开心!虽然2048真心不难,不过还是遇到了几个问题.) 小梦开学就大三了,不是专业的计算机或软件的专业学生.大一上学习了C语言,第一次感受到了编程的美妙,十分感谢谭浩强老师,那本C语言红皮书真心挺棒的,把书上的例子都亲手敲了一遍,然后把习题都做了一遍.之后了解过C++,学过一阵网页前段开发,因为我大一下就有网站了(当然,不是自己写的,用的都是wordpress).后来开始学C#,WPF,windows 8,一直到现在的windows p

网络小助手项目开发总结报告

网络小助手项目开发总结报告 1引言 1.1编写目的 通过一段时间后对网络小助手项目进行详细开发和测试,该项目已经基本完成.本报告旨在将项目开发过程中的经验和不足之处进行总结,为以后的团队开发活动积累经验.本报告经审核后,交由软件工程老师王老师审查. 1.2背景 说明: a.  软件项目名称:网络小助手 b.  开发者:兰梦,李金吉,马翔,赵天,胡佳奇 用户:学生 系统运行环境:windows XP/windows 7/windows 8下的通用PC 1.3参考资料 <软件工程--原理,方法与应用

【Qt编程】基于Qt的词典开发系列--后序

从去年八月份到现在,总算完成了词典的编写以及相关技术文档的编辑工作.从整个过程来说,文档的编写比程序的实现耗费的时间更多.基于Qt的词典开发系列文章,大致包含了在编写词典软件过程中遇到的技术重点与难点.每篇文章都完成了一个小的功能,所给的代码都基本上是可以独立运行的.本系列文章对于想要自己动手完成词典软件的程序员来说具有很好的参考价值,对于想要编写其它软件的人来说也具有参考意义. 词典软件制作的初衷 在2013的年终总结中,我提过想要学习一门界面编程语言,后来就选中了Qt.于是在2014年上半年

【Qt编程】基于Qt的词典开发系列&lt;三&gt;--开始菜单的设计

这篇文章讲讲如何实现开始菜单(或者称为主菜单)的设计.什么是开始菜单呢?我们拿常用的软件来用图例说明,大多数软件的开始菜单在左下角,如下图: 1.window 7的开始菜单 2.有道词典的主菜单 3.QQ的开始菜单 4.我写的词典软件的开始菜单 当你左键单击开始菜单时,就会弹出相应的菜单选项,然后你就可以进行相关操作.本文只讲如何实现点击按钮,弹出菜单功能,至于点击菜单后的事件需要你自己编写.当然,关于右击按钮出现菜单的方法,则是要重写qt自带的函数,至于具体操作可以百度. 要想使按钮实现左键单

小程序连开发个日记功能都要企业认证。。。

学了十几天小程序云开发,自己写了个小项目练练手,本想着写完之后发布上线,让别人也可以体验该小程序,结果只有我一个人使用,真的是服了 觉得小程序还是没有flutter,RN等原生开发来的舒服,也没有H5混合式开法来的爽,(前提是不企业认证)也就这样了,自己写的项目自己欣赏吧, 下面是失败原因: 这原因我真的是无语了,亏我开发还考虑了不同用户的使用 下面奉上小程序的一些功能 小程序做的也不怎么漂亮 还有一些todo,地图,解梦,看运势等功能 因为让不同用户使用,所以不知道用户数据隔离这一块有没有bu

金华网络公司微信应用号小程序制作开发

微信应用号小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想(让手机不需要更多的内存用于安装一堆无用的app客户端),用户扫一扫或者搜一下即可打开应用.也体现了“用完即走”的理念,用户不用关心是否安装太多应用的问题. 应用将无处不在,随时可用,但又无需安装卸载. 基本介绍 微信将应用号“小程序”定义为“一种新的应用形态”(这是让我们可以连接所有我们工作.生活.娱乐所有需要的一种简单的形态).微信方面强调,小程序(应用号).订阅号.服务号.企业号目前是并行的体系. 微信应用号