Android筆記(五十三) 利用有道OPENAPI做簡單的翻譯demo

來源:互聯網
上載者:User

標籤:

  先去 http://fanyi.youdao.com/openapi?path=data-mode 申請開發人員key

  有道api會自動將申請的單詞翻譯並返回為xml或者json格式,我們所需要做的就是將返回的格式顯示到螢幕上即可

MainActivity.java

package cn.lixyz.youdaodictionary;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {    private EditText word;    private Button submit;    private TextView translation;    private String str = "http://fanyi.youdao.com/openapi.do?keyfrom=AndroidHttpTest&key=507293865&type=data&doctype=json&version=1.1&q=";    String requestStr;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        submit.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                String tmpWord = word.getText().toString().trim();                requestStr = str + tmpWord;                new Thread(new Runnable() {                    @Override                    public void run() {                        URL url;                        try {                            url = new URL(requestStr);                            URLConnection connection = url.openConnection();                            InputStream is = connection.getInputStream();                            InputStreamReader isr = new InputStreamReader(is, "UTF-8");                            BufferedReader br = new BufferedReader(isr);                            String tmpLine = "";                            String line = null;                            while ((line = br.readLine()) != null) {                                tmpLine = tmpLine + line;                            }                            Message msg = new Message();                            msg.obj = tmpLine;                            handler.sendMessage(msg);                        } catch (MalformedURLException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        } catch (IOException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        }                    }                }).start();            }        });    }    private void initView() {        word = (EditText) findViewById(R.id.word);        submit = (Button) findViewById(R.id.submit);        translation = (TextView) findViewById(R.id.translation);    }    Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            // translation.setText(msg.obj.toString());            try {                JSONObject object = new JSONObject(msg.obj.toString());                JSONObject jsonobject = object.getJSONObject("basic");                String usPhonetic = jsonobject.getString("us-phonetic");                String phonetic = jsonobject.getString("phonetic");                String ukPhonetic = jsonobject.getString("uk-phonetic");                JSONArray array = jsonobject.getJSONArray("explains");                String showText = "";                for (int i = 0; i < array.length(); i++) {                    String obj = array.getString(i);                    showText = showText + obj + "\n";                }                showText = "美音:\t" + usPhonetic + "\n" + "英音:\t" + ukPhonetic + "\n" + "\n\n" + showText;                translation.setText(showText);            } catch (JSONException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        };    };}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="cn.lixyz.youdaodictionary.MainActivity" >    <EditText        android:id="@+id/word"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp" />    <Button        android:id="@+id/submit"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="翻譯" />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:text="翻譯為:" />    <TextView        android:id="@+id/translation"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10dp" /></LinearLayout>

  因為要請求網路連接,所以需要加上INTENET許可權

<uses-permission android:name="android.permission.INTERNET"/>

 

Android筆記(五十三) 利用有道OPENAPI做簡單的翻譯demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.