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="" id="" num=""> <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 = ;//缓冲的最大字节数
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() <= )
{
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() == )
{
spPron.Visibility = Visibility.Visible;
List<XElement> psList = pss.ToList();
txtPsUK.Text = "英:" + "[" + psList[].Value + "]";
txtPsUs.Text = "美:" + "[" + psList[].Value + "]";
}
else if (pss.Count() == )
{
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() == )
{
List<XElement> pronlist = prons.ToList();
mePronUK.Source = new Uri(pronlist[].Value);
mePronUs.Source = new Uri(pronlist[].Value);
btnPronUK.Visibility = Visibility.Visible;
btnPronUs.Visibility = Visibility.Visible;
}
else if (prons.Count() == )
{
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() > )
{
foreach (XElement pos in poss)
{
posList.Add(pos.Value);
}
} IEnumerable<XElement> acceptations = dict.Elements(XName.Get("acceptation"));
spAcceptions.Children.Clear();
if (acceptations.Count() > )
{
int i = ;
foreach (XElement acceptation in acceptations)
{
TextBlock textAcceptation = new TextBlock();
textAcceptation.FontSize = ;
textAcceptation.TextWrapping = TextWrapping.Wrap;
textAcceptation.Margin = new Thickness();
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() > )
{
foreach (XElement sent in sents)
{
XElement orig = sent.Element(XName.Get("orig"));
TextBlock textOrig = new TextBlock();
textOrig.FontSize = ;
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 = ;
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();
}
}
}
源代码下载:
WP8.1小梦词典开发1:金山词霸API使用的更多相关文章
- WP8.1小梦词典开发2:百度翻译API使用
原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...
- 小梦词典WP8.1应用发布
这几天一直在做这款应用,今天终于发布了! 小梦词典简介: 小梦词典是一款永久免费无广告的网络词典. 支持英汉单词查询: 支持中,英,法,韩,德,俄,日七国语言翻译,多语言极致体验: 支持生词本记忆,查 ...
- 小程序·云开发的HTTP API调用丨实战
小程序云开发之httpApi调用. 小程序云开发之httpApi调用(返回"47001处理") 技术栈 采用 nodejs + express 搭建web服务器,采用 axios ...
- 小程序语音红包开发中 汉字转拼音的问题 微信小程序红包开发遇到的坑
公司最近在开发微信小程序的红包功能,语音红包需要用到文字转拼音的功能. 之前介绍过怎么将中文的汉字转为拼音的,具体看下面这篇文章. 微信语音红包小程序开发如何提高精准度 红包小程序语音识别精准度 微信 ...
- 【Qt编程】基于Qt的词典开发系列--后序
从去年八月份到现在,总算完成了词典的编写以及相关技术文档的编辑工作.从整个过程来说,文档的编写比程序的实现耗费的时间更多.基于Qt的词典开发系列文章,大致包含了在编写词典软件过程中遇到的技术重点与难点 ...
- 【Qt编程】基于Qt的词典开发系列<三>--开始菜单的设计
这篇文章讲讲如何实现开始菜单(或者称为主菜单)的设计.什么是开始菜单呢?我们拿常用的软件来用图例说明,大多数软件的开始菜单在左下角,如下图: 1.window 7的开始菜单 2.有道词典的主菜单 3. ...
- 【Qt编程】基于Qt的词典开发系列<十五>html特殊字符及正则表达式
1.html特殊字符的显示 我们知道html语言和C语言一样也有一些特殊字符,它们是不能正常显示的,必须经过转义,在网上可以查到如何显示这些字符,如下图所示: 上图给了最常用的特殊字符的显示,下面我们 ...
- C#开发微信门户及应用(22)-微信小店的开发和使用
在做企业电子商务方面,微信小店虽然较淘宝天猫等起步较晚,但是作为一个电商平台,这个影响力不容忽视,结合微信的特点和便利,微信小店具有很好的粘合性和广泛的用户基础,因此花费一定的时间,在这方面做深入的研 ...
- 微信小程序代开发
微信申请第三方之后可以获取授权方的很多权限,主要的是生码和待开发,生码的第三方授权之前已经写了一篇文章,最近做了小程序待开发,总结一下写下来供大家参考 注意事项:如果在调试过程中返回了错误码请到小程序 ...
随机推荐
- 字符集编码与Python(二)Unicode与utf-8
Python中的Unicode和utf-8 上一篇提过了字符集的历史其中简单的讲解了Unicode与utf-8的关系,简单的总结一下: utf-8和utf-16 .utf-32是一类,实现的功能是 ...
- C#操作XML方式
前言 前一篇XML读取,现在咱们继续XML操作相关 C#中也有三种操作(增.删.改.查)XML文件方法如下: 使用 XmlDocument(DOM模式) 使用 XmlTextWriter(流模式) 使 ...
- 下一个项目为什么要用 SLF4J
阿里巴巴 Java 开发手册 前几天阿里巴巴在云栖社区首次公开阿里官方Java代码规范标准,就是一个PDF手册,有命名规范,让你知道自己原来取的每一个类名.变量名都是烂名字,真替你家未来孩子担心:有集 ...
- 爬虫之爬取网贷之家在档P2P平台基本数据并存入数据库
python 版本 :3.5.2 Jupyter Notebook 使用库: reuqests (For human) json (用来加载JSON数据) datetime (用来记录抓取所花时间,也 ...
- contenteditable实现可编辑的HTML标签
最近工作中遇到了一个小问题,让我学到了新的标签属性--contenteditable. 我需要实现的是手机端界面,特别简单的一个页面,如下图: 在我脑海里第一时间想到的应该就是一个form表单然后里面 ...
- [UWP]附加属性2:实现一个Canvas
5. 附加属性实践:自定义Canvas 附加属性在UWP中是一个十分重要的组成部分,很多功能都依赖于附加属性实现,典型的例子是常用的Grid和Canvas.通常附加属性有三个使用场景:插入属性.触发行 ...
- C#基础笔记1
1>>数据类型: Int double:小数 char:字符型,只能存储一个字符,并且存储的这个字符要用单引号引起来.如:'a'; string:字符串,可以储存多个字符,用双引号引起来( ...
- jQuery_第三章_工厂函数
- Dockerfile详解
Dockerfile详解 利用Dockerfile文件,可以构建docker的image镜像 命令使用 通过-f参数指定Dockerfile路径,进行构建image docker build -f / ...
- Linux-7.2+LNMP+zabbix-3.2.1
LNMP+zabbix-3.2.1 一.zabbix服务端部署 1.解压 yum –y install bzip2 tar -xf nginx-1.10.1.tar.gz tar -xf php-5. ...