文章评分
次,平均分 :
[收起]
文章目录
由于百度翻译、有道翻译等翻译服务都开放了自己的API,开发者可以使用它们提供的API很简单就能开发出自己的翻译软件。自己开发的软件可能在功能上不及它们强大,但是翻译的结果是一样的,同时不会内置其它偷跑流量的进程,同时界面也相对简洁,随心所欲地进行修改。
首先在有道API进行申请API ID和KEY后,将下列程序中clientID和clientSecret替换为ID和KEY,通过GET请求获得字符串进行解析。下面是我使用java解析JSON数据的代码:
Android(Java)解析有道翻译API-Json数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
private String clientID="你在有道API申请的ID"; private String clientSecret="你在有道API申请的密钥"; Handler errHander = new Handler() { @Override public void handleMessage(Message msg) { String notice = ""; switch (msg.what) { case 20: notice = "要翻译的文本过长"; break; case 30: notice = "无法进行有效的翻译"; break; case 40: notice = "不支持的语言类型"; break; case 50: notice = "无效的key"; break; case 60: notice = "无词典结果"; break; } Toast.makeText(MainActivity.this, notice, Toast.LENGTH_SHORT); } }; Handler hander = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x123) { toString.setText(reply); } else if (msg.what == 0x124) { Toast.makeText(MainActivity.this, "查询失败,请检查联网", Toast.LENGTH_SHORT).show(); } } }; private Runnable askForYouDao = new Runnable() { @Override public void run() { try { String url_path = "http://fanyi.youdao.com/openapi.do?keyfrom=" + clientID + "&key=" + clientSecret + "&type=data&doctype=json&version=1.1&q=" + URLEncoder.encode(keywords, "utf8"); URL getUrl = new URL(url_path); HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection(); connection.setConnectTimeout(3000); connection.connect(); BufferedReader replyReader = new BufferedReader( new InputStreamReader(connection.getInputStream(), "utf-8"));//约定输入流的编码 reply = replyReader.readLine(); JSONObject replyJson = new JSONObject(reply); String errorCode = replyJson.getString("errorCode"); if (errorCode.equals("0")) { String query = replyJson.getString("query"); JSONArray translation = replyJson.has("translation") ? replyJson.getJSONArray("translation") : null; JSONObject basic = replyJson.has("basic") ? replyJson.getJSONObject("basic") : null; JSONArray web = replyJson.has("web") ? replyJson.getJSONArray("web") : null; String phonetic=null; String uk_phonetic=null; String us_phonetic=null; JSONArray explains=null; if(basic!=null){ phonetic=basic.has("phonetic")? basic.getString("phonetic"):null; uk_phonetic=basic.has("uk-phonetic")? basic.getString("uk-phonetic"):null; us_phonetic=basic.has("us-phonetic")? basic.getString("us-phonetic"):null; explains=basic.has("explains")? basic.getJSONArray("explains"):null; } // if(web!=null){ // JSONArray webs=web.getJSONObject() // } String translationStr=""; if(translation!=null){ translationStr="\n翻译:\n"; for(int i=0;i<translation.length();i++){ translationStr+="\t【"+(i+1)+"】"+translation.getString(i)+"\n"; } } String phoneticStr=(phonetic!=null? "\n发音:"+phonetic:"") +(uk_phonetic!=null? "\n英式发音:"+uk_phonetic:"") +(us_phonetic!=null? "\n美式发音:"+us_phonetic:""); String explainStr=""; if(explains!=null){ explainStr="\n\n释义:\n"; for(int i=0;i<explains.length();i++){ explainStr+="\t【"+(i+1)+"】"+explains.getString(i)+"\n"; } } reply="原文:"+query+"\n"+translationStr+phoneticStr+explainStr; hander.sendEmptyMessage(0x123); } else { // Message errorMsg=new Message(); int what = Integer.parseInt(errorCode); // errorMsg.what=what; errHander.sendEmptyMessage(what); } } catch (Exception e) { Log.e("errss", e.getMessage()); hander.sendEmptyMessage(0x124); } } }; |

关注我的微信,获取文章更新
如果你觉得这篇文章对你有用,可以点击下面的“赞助作者”打赏作者!
转载注明原文出处:王柏元的博客>>https://wangbaiyuan.cn/android-java-parsing-api-youdao-translation-json-data.html
暂无评论