星期四, 六月 26, 2008

My daughter was born on May 23, 2008

Welcome to my daughter's photo album: xuyichun.yupoo.com
Here is a piece of her photos

星期二, 十二月 25, 2007

DictDroid (English<->Chinese)

What will it look like:
DictDroid


What will it support:
English to Chinese, or Chinese to English (current Android is unable to input Chinese)

The Full Source:
/res/layout/main.xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7<TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="Input a word:"
11 />
12
13<EditText id="@+id/edit1"
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:hint="English or Chinese word"
17 android:singleLine="true"
18 android:numeric="false"
19 />
20<Button id="@+id/button1"
21 android:layout_width="fill_parent"
22 android:layout_height="wrap_content"
23 android:text="Consult"
24 />
25<TextView
26 android:layout_width="fill_parent"
27 android:layout_height="wrap_content"
28 android:text="Result:"
29 />
30
31<EditText id="@+id/edit2"
32 android:layout_width="fill_parent"
33 android:layout_height="fill_parent"
34 android:layout_weight="1"
35 />
36
37</LinearLayout>
38


/res/values/string.xml
1<?xml version="1.0" encoding="utf-8"?>
2<resources>
3 <string name="app_name">Dictionary</string>
4 <string name="api_url">http://dict.cn/ws.php?q=</string>
5 <string name="consult">Consult</string>
6 <string name="sentences">例句与用法:</string>
7</resources>
8


/src/your_package_structure/DictDroid.java
1package com.anthony.DictDroid;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.InputStreamReader;
6import java.io.Reader;
7import java.io.UnsupportedEncodingException;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import javax.xml.parsers.DocumentBuilder;
11import javax.xml.parsers.DocumentBuilderFactory;
12
13import org.w3c.dom.Document;
14import org.w3c.dom.Element;
15import org.w3c.dom.NodeList;
16import org.xml.sax.InputSource;
17
18import android.app.Activity;
19import android.os.Bundle;
20import android.util.Log;
21import android.view.View;
22import android.view.View.OnClickListener;
23import android.widget.Button;
24import android.widget.EditText;
25
26public class DictDroid extends Activity {
27 /** Called when the activity is first created. */
28 private final String TAG = "DictDroid";
29
30 @Override
31 public void onCreate(Bundle icicle) {
32 super.onCreate(icicle);
33 setContentView(R.layout.main);
34
35 ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
36
37 @Override
38 public void onClick(View v) {
39 EditText queryWord = (EditText)findViewById(R.id.edit1);
40 String query = queryWord.getText().toString().trim();
41 if (query.equals("")) {
42 queryWord.setText("");
43 queryWord.requestFocus();
44 return;
45 }

46
47 ((Button)v).setText(getText(R.string.consult)+"...");
48
49 EditText responseContent = (EditText)findViewById(R.id.edit2);
50 responseContent.setText("");
51
52 try {
53 String queryStr;
54
55 if (isEnglish(query)) {
56 queryStr = query.replace(" ", "%20");
57 }
else {
58 queryStr = encode2Gbk(query);
59 }

60
61 Log.i(TAG, getText(R.string.api_url)+queryStr);
62 URL url = new URL(getText(R.string.api_url)+queryStr);
63 HttpURLConnection http = (HttpURLConnection)url.openConnection();
64 http.setRequestMethod("GET");
65 http.connect();
66
67 InputStream inputstream = null;
68
69 try {
70 inputstream = http.getInputStream();
71 }
catch (IOException e) {
72 Log.e(TAG, "getInputStream error");
73 }

74
75 Reader reader = null;
76 InputSource inputsource = null;
77 try {
78 reader = new InputStreamReader(inputstream, "gb2312");
79 }
catch (UnsupportedEncodingException e) {
80 Log.e(TAG, "new InputStreamReader UnsupportedEncodingException");
81 }
catch (Exception e) {
82 Log.e(TAG, "new InputStreamReader error");
83 }

84
85 try {
86 inputsource = new InputSource(reader);
87 }
catch (Exception e) {
88 Log.e(TAG, "new InputSource error");
89 }

90
91 try{
92 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
93 DocumentBuilder db = dbf.newDocumentBuilder();
94 Document doc = db.parse(inputsource);
95
96 // pron
97 NodeList pronList = doc.getElementsByTagName("pron");
98 Log.i(TAG, "pron count: "+pronList.getLength());
99
100 String prons = query+":";
101 for (int i=0; i<pronList.getLength(); i++) {
102 Element pronEl = (Element)doc.getElementsByTagName("pron").item(i);
103 String aPron = pronEl.getChildNodes().item(0).getNodeValue();
104 Log.i(TAG, "pron["+i+"]: "+aPron);
105 prons += " ["+aPron+"]";
106 }

107
108 // def
109 NodeList defList = doc.getElementsByTagName("def");
110 Log.i(TAG, "def count: "+defList.getLength());
111
112 String defs = "";
113 for (int i=0; i<defList.getLength(); i++) {
114 Element defEl = (Element)doc.getElementsByTagName("def").item(i);
115 String aDef = defEl.getChildNodes().item(0).getNodeValue();
116 Log.i(TAG, "def["+i+"]: "+aDef);
117 defs += aDef;
118 }

119
120 // sent
121 NodeList sentList = doc.getElementsByTagName("sent");
122 Log.i(TAG, "sent count: "+sentList.getLength());
123
124 String sents = getText(R.string.sentences).toString();
125 for (int i=0; i<sentList.getLength(); i++) {
126 Element sentEl = (Element)doc.getElementsByTagName("sent").item(i);
127 String orig =
128 sentEl.getElementsByTagName("orig").item(0).getChildNodes().item(0).getNodeValue().replace("\n", " ").trim();
129 String trans =
130 sentEl.getElementsByTagName("trans").item(0).getChildNodes().item(0).getNodeValue().replace("\n", " ").trim();
131 Log.i(TAG, "orig["+i+"]: "+orig);
132 Log.i(TAG, "trans["+i+"]: "+trans);
133 int j = i+1;
134 String aSent = "\n"+j+". "+orig+"\n "+trans;
135 sents += aSent;
136 }

137
138 String atall = prons + "\n\n" + defs + "\n\n" + sents.toString();
139 responseContent.setText(atall);
140 ((Button)v).setText(getText(R.string.consult));
141 }
catch (Exception e){
142 Log.e(TAG, "Exception 1: "+e.toString());
143 }

144 }
catch (Exception e){
145 Log.e(TAG, "Exception 2: "+e.toString());
146 }

147 }

148 }
);
149 }

150
151 public boolean isEnglish(String source) {
152 int unicode = source.codePointAt(0);
153 if ((unicode>='A' && unicode<='Z') || (unicode>='a' && unicode<='z') || unicode==' ') {
154 return true;
155 }
else {
156 return false;
157 }

158 }

159
160 public String encode2Gbk(String source) throws Exception {
161 for (int i=0; i<source.length(); i++) {
162 int unicode = source.codePointAt(i);
163 if ((unicode>='A' && unicode<='Z') || (unicode>='a' && unicode<='z')) {
164 throw new Exception("The string needn't do encoding");
165 }

166 }

167
168 byte[] queryBytes = source.getBytes("gb2312");
169 String destination = "";
170 for (int i=0; i<queryBytes.length; i++) {
171 String temp = Integer.toHexString(queryBytes[i]);
172 destination += "%"+temp.substring(temp.length()-2);
173 }

174
175 return destination;
176 }

177}