原文出自: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使用的更多相关文章

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

    原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...

  2. 小梦词典WP8.1应用发布

    这几天一直在做这款应用,今天终于发布了! 小梦词典简介: 小梦词典是一款永久免费无广告的网络词典. 支持英汉单词查询: 支持中,英,法,韩,德,俄,日七国语言翻译,多语言极致体验: 支持生词本记忆,查 ...

  3. 小程序·云开发的HTTP API调用丨实战

    小程序云开发之httpApi调用. 小程序云开发之httpApi调用(返回"47001处理") 技术栈 采用 nodejs + express 搭建web服务器,采用 axios ...

  4. 小程序语音红包开发中 汉字转拼音的问题 微信小程序红包开发遇到的坑

    公司最近在开发微信小程序的红包功能,语音红包需要用到文字转拼音的功能. 之前介绍过怎么将中文的汉字转为拼音的,具体看下面这篇文章. 微信语音红包小程序开发如何提高精准度 红包小程序语音识别精准度 微信 ...

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

    从去年八月份到现在,总算完成了词典的编写以及相关技术文档的编辑工作.从整个过程来说,文档的编写比程序的实现耗费的时间更多.基于Qt的词典开发系列文章,大致包含了在编写词典软件过程中遇到的技术重点与难点 ...

  6. 【Qt编程】基于Qt的词典开发系列<三>--开始菜单的设计

    这篇文章讲讲如何实现开始菜单(或者称为主菜单)的设计.什么是开始菜单呢?我们拿常用的软件来用图例说明,大多数软件的开始菜单在左下角,如下图: 1.window 7的开始菜单 2.有道词典的主菜单 3. ...

  7. 【Qt编程】基于Qt的词典开发系列<十五>html特殊字符及正则表达式

    1.html特殊字符的显示 我们知道html语言和C语言一样也有一些特殊字符,它们是不能正常显示的,必须经过转义,在网上可以查到如何显示这些字符,如下图所示: 上图给了最常用的特殊字符的显示,下面我们 ...

  8. C#开发微信门户及应用(22)-微信小店的开发和使用

    在做企业电子商务方面,微信小店虽然较淘宝天猫等起步较晚,但是作为一个电商平台,这个影响力不容忽视,结合微信的特点和便利,微信小店具有很好的粘合性和广泛的用户基础,因此花费一定的时间,在这方面做深入的研 ...

  9. 微信小程序代开发

    微信申请第三方之后可以获取授权方的很多权限,主要的是生码和待开发,生码的第三方授权之前已经写了一篇文章,最近做了小程序待开发,总结一下写下来供大家参考 注意事项:如果在调试过程中返回了错误码请到小程序 ...

随机推荐

  1. C# 连接 SQLServer 及操作

    随笔:连接: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=d ...

  2. css单位总结

    body的font-size:14px     body第二代子元素的font-size: em: 1.2em=1.2*1.2*14px rem:1.2rem=1.2*14px 视口高度:1000px ...

  3. js获取当前时间戳

    当使用js时我们可以使用timestamp = (new Date()).valueOf();直接获取当前时区时间点的时间戳.注意:js中时间戳的单位是毫秒,而php中则是秒

  4. dotnet new 命令使用模板

    dotnet new 命令使用模板快速生成单页应用. 最新版.NET Core SDK RC4 最大改动是更新了 dotnet new 命令. dotnet new 默认不再创建控制台应用,而是展示帮 ...

  5. 网易云直播SDK使用总结

    前言: 最近公司的项目中加入中直播这部分的功能,现在的直播平台真的很多很多,以前在朋友圈看到过这张图片,没办法一次性给大家看,就只能这样截成几张给大家看看.其实按照我自己的看法,现在的直播已经没办法做 ...

  6. 使用jQuery快速高效制作网页交互特效

    第四章:JQuery选择器 1.Jquery选择器简介 (1) Jquery中的选择器完全继承了CSS的风格,利用Jquery选择器,可以非常便捷和快速的找出特定的Dom元素,然后为他们添加相应的行为 ...

  7. P177 test 6-3 UVa536

    //P177 test 6-3 #include<cstdio> #include<cstring> using namespace std; +],s2[+]; int re ...

  8. 【转】Netty系列之Netty并发编程分析

    http://www.infoq.com/cn/articles/netty-concurrent-programming-analysis

  9. 每天一个linux命令(44)--ss命令

    ss 是 socket statistics 的缩写.顾名思义,ss 命令可以用来获取socket 统计信息,它可以显示和netstat 类似的内容.但 ss 的优势在于它能够显示更多更详细的有关TC ...

  10. C++ 11和C++98相比有哪些新特性

    此文是如下博文的翻译: https://herbsutter.com/elements-of-modern-c-style/ C++11标准提供了许多有用的新特性.这篇文章特别针对使C++11和C++ ...