Android笔记(五十三) 利用有道OPENAPI做简单的翻译demo-LMLPHP

  先去 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"/>
05-11 13:48