標籤:
先去 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