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)-微信小店的开发和使用
在做企业电子商务方面,微信小店虽然较淘宝天猫等起步较晚,但是作为一个电商平台,这个影响力不容忽视,结合微信的特点和便利,微信小店具有很好的粘合性和广泛的用户基础,因此花费一定的时间,在这方面做深入的研 ...
- 微信小程序代开发
微信申请第三方之后可以获取授权方的很多权限,主要的是生码和待开发,生码的第三方授权之前已经写了一篇文章,最近做了小程序待开发,总结一下写下来供大家参考 注意事项:如果在调试过程中返回了错误码请到小程序 ...
随机推荐
- 《JAVASCRIPT高级程序设计》Canvas绘图-2D上下文
Canvas是HTML5添加的新元素,这个元素负责在页面中设定一个区域,然后通过JavaScript动态的在这个区域绘制图形.<canvas>由几组API组成,除了具备基本绘图能力的2D上 ...
- (@WhiteTaken)设计模式学习——简单工厂
最近工作比较忙,所以没有怎么写博客,这几天将集中学习一下(厉风行)讲解的设计模式的相关知识,并对主要的代码进行介绍. 言归正传,接下来介绍最简单也是最基础的简单工厂设计模式. 什么是简单工厂? 简单工 ...
- sass 基础——回顾
1.webstorm 自动编译SASS 下载安装包 http://rubyinstaller.org/downloads/ 然后点击安装,路径为默认路径就行, 勾选以下两项 add Ruby exec ...
- CocoaPods 2016最新安装和使用说明
cocoapods 简介: CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods本身所 ...
- pyqt的 .ui 转换为 .py 后的操作注意事项
1. 增加 import sys 2. 将 Ui_MainWindow(object) 中的 object 修改成修改成 QtGui.QMainWindow 3. 在 Ui_MainWindow 类中 ...
- 怎么在ubuntu上运行php代码?
1. 首先,你需要安装Apache2. sudo apt-get update sudo apt-get install apache2 当安装完以后,Apache就已经开始运行啦,你可以进行测试,通 ...
- 【Scala】Scala之Classes and Properties
一.前言 前面学习了控制结构,下面学习Scala的Class和Properties. 二.Class&Properties 尽管Scala和Java很类似,但是对类的定义.类构造函数.字段可见 ...
- 从C#到TypeScript - async await
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- iOS 手机摇一摇功能
调用手机摇一摇功能其实很简单,在你调用的控制器的 viewDidLoad方法里调用 [UIApplication sharedApplication].applicationSupportsShake ...
- 关于synchronized、wait、notify已经notifyAll的使用
前言:关于synchronized.wait.notify已经notifyAll大家应该不陌生,现在我大致说一下我的理解. 一:synchronized synchronized中文解释是同步,那么什 ...