/**
*
*/
package com.joyband.tranlator.ws;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import com.joyband.tranlator.model.WordItem;
import com.joyband.utils.StringUtil;
/**
* @author Joyband
*
*/
public class TranslatorHelper {
public static String[] METHOD_NAMES = { "Translator", "TranslatorString",
"TranslatorReferString", "TranslatorSentenceString", "SuggestWord",
"GetMp3" };
private static final String TRANSLATOR = "Translator";
private static final String TRANSLATOR_STRING = "TranslatorString";
private static final String TRANSLATOR_REFER_STRING = "TranslatorReferString";
private static final String TRANSLATOR_SENTENCE_STRING = "TranslatorSentenceString";
private static final String SUGGEST_WORD = "SuggestWord";
private static final String GETMP3 = "GetMp3";
public static WordItem queryByStep(String wordKey) {
wordKey = StringUtil.encodeString(wordKey, "UTF-8");
WordItem word = new WordItem();
try {
// TranslatorString
word = translatorString(wordKey);
// TranslatorReferString
String[] referArray = translatorReferString(wordKey);
if (referArray != null && referArray.length > 0
&& !referArray[0].equals("- Empty -")) {
ArrayList<String> referList = new ArrayList<String>();
for (String s : referArray)
referList.add(s);
word.setReferList(referList);
}
// TranslatorSentenceString
referArray = translatorSentenceString(wordKey);
if (referArray != null && referArray.length > 0
&& !referArray[0].equals("- Empty -")) {
ArrayList<Map<String, String>> sentences = new ArrayList<Map<String, String>>();
for (String s : referArray) {
Map<String, String> map = new HashMap<String, String>();
StringTokenizer st = new StringTokenizer(s, "|");
map.put("Orig", st.nextToken());
map.put("Trans", st.nextToken());
sentences.add(map);
}
word.setSentences(sentences);
}
// SuggestWord
referArray = suggestWord(wordKey);
if (referArray != null && referArray.length > 0
&& !referArray[0].equals("- Empty -")) {
ArrayList<String> suggestWords = new ArrayList<String>();
for (String s : referArray)
suggestWords.add(s);
word.setSuggestWords(suggestWords);
}
// GetMp3
// 这里暂不实现,到调用时机时直接调用返回byte数组的方法,或者直接访问URL
} catch (Exception ex) {
ex.printStackTrace();
}
return word;
}
/**
* Translator 方法
* 通过输入中文或英文单词进行双向翻译,输入参数 wordKey = 字符串,中文或英文单词, 返回名称为:“Dictionary”的 DataSet。
* 此 Dataset 包含三个 DataTable。
* DataTable(0) 表名称:Trans,中英文基本翻译,包含一个 Row,5 个 Item
*
* @param wordKey
* @return
* @throws Exception
*/
public static WordItem translator(String wordKey) throws Exception {
wordKey = StringUtil.encodeString(wordKey, "UTF-8");
WordItem word = new WordItem();
Map<String, Object> params = new HashMap<String, Object>();
params.put("wordKey", wordKey);
String returnString = WSUtil.callWebService(
TranslatorHelper.TRANSLATOR, params);
System.out.println("returnString=" + returnString);
parseWordItem(returnString, word);
// SuggestWord
String[] referArray = suggestWord(wordKey);
if (referArray != null && referArray.length > 0
&& !referArray[0].equals("- Empty -")) {
ArrayList<String> suggestWords = new ArrayList<String>();
for (String s : referArray)
suggestWords.add(s);
word.setSuggestWords(suggestWords);
}
return word;
}
/**
* TranslatorString 方法
* 通过输入中文或英文单词获得基本翻译,输入参数 wordKey = 字符串,中文或英文单词,
* 返回为:一维数组,String[0] ---- String[4]。
* String[0] = 需要进行翻译的单词(输入的单词)
* String[1] = 音标(英文)、拼音(中文字)
* String[2] = 中文字的国标码、部首、笔画、笔顺信息
* String[3] = 翻译、解释,多个翻译使用中文“;”分隔
* String[4] = 英文单词朗读 Mp3 文件名
*
* 如果没有发现单词翻译或出现错误,String[3] 会出现以下提示:
* WordKey Empty ---- 没有输入单词
* Not Found ---- 不能翻译
* Error ---- 系统错误
* Not Data ---- 没有输据
*
* @param wordKey
* @return 返回为:一维数组,String[0] ---- String[4]
* @throws Exception
*/
public static WordItem translatorString(String wordKey) throws Exception {
String[] resultArray = null;
WordItem word = new WordItem();
;
Map<String, Object> params = new HashMap<String, Object>();
params.put("wordKey", wordKey);
String returnString = WSUtil.callWebService(
TranslatorHelper.TRANSLATOR_STRING, params);
//System.out.println("returnString=" + returnString);
resultArray = parseResult(returnString);
if (resultArray != null) {
word.setWordKey(resultArray[0]);
word.setPron(resultArray[1]);
word.setInfo(resultArray[2]);
word.setTranslation(resultArray[3]);
if (resultArray[3].contains("WordKey Empty")
|| resultArray[3].contains("Not Found")
|| resultArray[3].contains("Error")
|| resultArray[3].contains("Not Data")) {
word.setErrorMsg("没有相关翻译内容");
} else {
String[] s3 = resultArray[3].split(";");
ArrayList<String> translations = new ArrayList<String>();
word.setTranslations(translations);
for (int i = 0; i < s3.length; i++) {
translations.add(s3[i]);
}
}
word.setMp3Name(resultArray[4]);
}
return word;
}
/**
* TranslatorReferString 方法
* 通过输入中文单词获得相关词条,输入参数 wordKey = 字符串,中文单词
* @param wordKey
* @return
* 返回为:一 维数组,String[0] ---- String[n]。
* 没有内容或没有结果返回提示: String[0] = - Empty -
* @throws Exception
*/
public static String[] translatorReferString(String wordKey)
throws Exception {
String[] resultArray = null;
Map<String, Object> params = new HashMap<String, Object>();
params.put("wordKey", wordKey);
String returnString = WSUtil.callWebService(
TranslatorHelper.TRANSLATOR_REFER_STRING, params);
if (returnString != null && returnString.length() > 0) {
resultArray = parseResult(returnString);
}
return resultArray;
}
/**
* TranslatorSentenceString 方法
* 通过输入中文或英文单词获得中译英的例句,输入参数 wordKey = 字符串,中文单词, 返回为:一维数组,String[0] ---- String[n]。
* 结构为:正本|译本。 (以“|”分隔符)
* 没有内容或没有结果返回提示: String[0] = - Empty -
* @param wordKey
* @return
* @throws Exception
*/
public static String[] translatorSentenceString(String wordKey)
throws Exception {
String[] resultArray = null;
Map<String, Object> params = new HashMap<String, Object>();
params.put("wordKey", wordKey);
String returnString = WSUtil.callWebService(
TranslatorHelper.TRANSLATOR_SENTENCE_STRING, params);
//System.out.println("returnString=" + returnString);
// anyType{string=anyType{}; string=anyType{}; string=anyType{};
// string=WordKey Empty; string=anyType{}; }
if (returnString != null && returnString.length() > 0) {
resultArray = parseResult(returnString);
}
return resultArray;
}
/**
* SuggestWord 方法
* 通过输入英文单词获得候选词,输入参数 wordKey = 字符串,英文单词,返回为:一维 数组,String[0] ---- String[n]。
* 没有内容或没有结果返回提示: String[
没有合适的资源?快使用搜索试试~ 我知道了~
android翻译源代码
共67个文件
class:24个
java:12个
xml:7个
5星 · 超过95%的资源 需积分: 50 9 下载量 83 浏览量
2014-07-01
19:00:20
上传
评论
收藏 1.31MB RAR 举报
温馨提示
可以运行的android翻译功能 源代码 功能非常齐全,适合初学者学习使用,注释详细。
资源推荐
资源详情
资源评论
收起资源包目录
Android翻译软件源代码.rar (67个子文件)
Translator
bin
classes.dex 236KB
com
joyband
tranlator
Main$2.class 2KB
ws
TranslatorHelper.class 8KB
WSUtil.class 4KB
R$layout.class 505B
About.class 1KB
ExplainAdapter.class 2KB
R$id.class 1002B
R$drawable.class 733B
model
WordItem.class 4KB
R$attr.class 349B
R.class 603B
Main.class 9KB
Main$4$1.class 774B
Main$1.class 2KB
SentenceAdapter.class 2KB
R$string.class 569B
Main$3.class 1KB
Main$4.class 2KB
R$color.class 399B
About$1.class 1KB
utils
Const.class 538B
StringUtil.class 2KB
DateTimeUtil.class 297B
IOUtils.class 4KB
Translator.apk 434KB
resources.ap_ 322KB
res
drawable-ldpi
speaker2.gif 263B
bkg_top.jpg 2KB
bkg_explain.jpg 2KB
icon.png 6KB
bkg_examples.jpg 1KB
refer.gif 671B
speaker1.gif 151B
explain.gif 478B
example.gif 487B
btn_search.jpg 2KB
bkg_refer.png 373B
drawable-hdpi
icon.png 6KB
values
strings.xml 562B
color.xml 103B
drawable-mdpi
icon.png 6KB
layout
explain_item.xml 419B
about.xml 2KB
main.xml 4KB
sentence_item.xml 616B
assets
font
lingoes.ttf 482KB
default.properties 364B
gen
com
joyband
tranlator
R.java 3KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 208B
src
com
joyband
tranlator
SentenceAdapter.java 1KB
ws
WSUtil.java 3KB
TranslatorHelper.java 14KB
model
WordItem.java 4KB
ExplainAdapter.java 1KB
About.java 1012B
Main.java 10KB
utils
StringUtil.java 2KB
IOUtils.java 3KB
Const.java 376B
DateTimeUtil.java 111B
.project 846B
.classpath 427B
lib
jdom.jar 62KB
ksoap2-android-assembly-2.4-jar-with-dependencies.jar 138KB
AndroidManifest.xml 814B
共 67 条
- 1
资源评论
- 卖火柴的little-boy2015-12-20代码不错,值得学习
gaoyong084625
- 粉丝: 1
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功