有道API在Android开发中的使用


  1. 申请有道API将获得以下链接

http://fanyi.youdao.com/openapi.do?keyfrom=mydictdemo&key=226680901&type=data&doctype=json&version=1.1&q=query
  • 使用此链接查询单词将得到一个JSON格式的文本
{
    "translation": [
        "查询"
    ],
    "basic": {
        "us-phonetic": "'kwɪri",
        "phonetic": "'kwɪərɪ",
        "uk-phonetic": "'kwɪərɪ",
        "explains": [
            "n. 疑问,质问;疑问号 ;[计] 查询",
            "vi. 询问;表示怀疑",
            "vt. 询问;对……表示疑问"
        ]
    },
    "query": "query",
    "errorCode": 0,
    "web": [
        {
            "value": [
                "查询",
                "疑问",
                "询问"
            ],
            "key": "Query"
        },
        {
            "value": [
                "空间查询",
                "空间查询",
                "空间索引"
            ],
            "key": "Spatial query"
        },
        {
            "value": [
                "闪回查询",
                "倒叙查询",
                "回闪查询"
            ],
            "key": "Flashback query"
        }
    ]
}

向服务器发送请求获取JSON格式文本

/**
 * 从指定的URL中获取文本
 * @param urlPath
 * @return
 * @throws Exception
 */
public static String readParse(String urlPath) throws Exception {  
   ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
   byte[] data = new byte[1024];  
    int len = 0;  
    URL url = new URL(urlPath);  
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
    InputStream inStream = conn.getInputStream();  
    while ((len = inStream.read(data)) != -1) {  
        outStream.write(data, 0, len);  
    }  
    inStream.close();  
    return new String(outStream.toByteArray());
    //通过out.Stream.toByteArray获取到写的数据  
}

解析JSON文本

String query = input.getText().toString();
//获取要查询的文本
english.setText(query);
pronuncation.setText("loading...");
chinese.setText("loading...");

Log.i("mytrace",query);

final String url = "http://fanyi.youdao.com/openapi.do?keyfrom=mydictdemo&key=226680901&type=data&doctype=json&version=1.1&q=" + query;

//新建线程来请求服务器以便于实现在后台查询
new Thread(new Runnable(){

    @Override
    public void run() {
        try {
            //获取json文本,并解析为JSONObject对象
            JSONObject result = new JSONObject(readParse(url)); 
            String pro = "";
            String exp = "";
            JSONObject basic = null;
            JSONArray explains = null;
            try {
                //尝试查找basic节点内容
                basic = new JSONObject(result.getString("basic"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                //尝试查找explains节点内容
                explains = new JSONArray(basic.getString("explains"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                //尝试查找音标
                pro = "美 " + "["+basic.getString("us-phonetic")+"]" + "\n";
                pro += "英 " + "["+basic.getString("uk-phonetic")+"]";
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                //读取explains节点内容
                for(int i=0;i<explains.length();i++){
                    Log.i("mytrace",explains.getString(i));
                    exp += explains.getString(i) +"\n";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            //更新UI,这里用到了Handler
            //Android禁止在非Activity主线程更新UI,所以要使用Handler
            updateUi = new UpdateUi(pro, exp);
            updateUiHandler.post(updateUi);

            } catch (JSONException e1) {
                e1.printStackTrace();
            } catch (Exception e1) {
                e1.printStackTrace();
            } 
    }

}).start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值