RSS Feed

改进版的Google 翻译前端

2009-07-06 by bborn

其实一直对android很感兴趣
虽然没有用过它的真机…

上个星期看到了这篇文章
Android:Google 翻译前端
作者的代码写的简单明了
并且发布了源代码给大家学习
真是很不错

看了之后 决定动手增加点东西 学习一下android的开发
增加的内容主要是在查询过程中弹出一个等待的对话框
涉及到的内容包括 ProgressDialog, AsyncTask
效果如下
当用户点击Translate的时候,会出现一个”正在查询”的对话框
查询完毕后对话框消失

对话框很好做 showDialog(1) 后
系统会调用

@Override
protected Dialog onCreateDialog(int id) {
if(1 == id)
{
ProgressDialog dialog = new ProgressDialog(GTranslator.this);
dialog.setTitle(“正在查询”);
dialog.setMessage(“请稍候……”);
return dialog;
}
return null;
}

但是 查询是个阻塞的过程
我们必须得在另外一个线程里去做这个事情
比较简单的方法就是继承AsyncTask
网上有一篇转载比较多的关于AsyncTask的文章(Android线程模型(Painless Threading))
官方的说明在这里 http://developer.android.com/reference/android/os/AsyncTask.html
大概说一下 android 的ui线程不是线程安全的
我们不能在其他的线程里直接做关于ui的操作
这里有个简单的办法就是继承AsyncTask
然后实现它的doInBackground方法
在类的onPreExecute(),publishProgress(Progress…),onPostExecute(Result)方法里
做ui的操作
如果在doInBackground里操作ui
可能会弹出异常提示
“Only the original thread that created a view hierarchy can touch its views”
不是每次都弹…为什么了?

下面贴一下线程的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	private class ProcessDialogThread extends AsyncTask<String, Void, String> {
		// @Override
		protected String doInBackground(String... str) {
 
			String toTranslateTextString = toTranslateEditText.getText()
					.toString();
			String tempString = toTranslateTextString.replace(" ", "%20");
			fromString = mCountries[fromSpinner.getSelectedItemPosition()];
			toString = mCountries[toSpinner.getSelectedItemPosition()];
			String queryString = tempString + "&langpair=" + fromString + "%7C"
					+ toString;
 
			return getRawData(queryString);
		}
 
		protected void onPreExecute() {
			showDialog(1);
			translatedTextView.setText("Connecting...");
		}
 
		protected void onPostExecute(String rawData) {
			dismissDialog(1);
			if (null != rawData) {
				String parsedDataString = getData(rawData);
				if (null == parsedDataString || "" == parsedDataString) {
					try {
						translatedTextView.setText("Not found");
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					translatedTextView.setText(parsedDataString);
					dbAdapter.insertItem(toTranslateEditText.getText()
							.toString(), parsedDataString);
 
					/*
					 * FileAccess.writeFile(this,
					 * toTranslateEditText.getText().toString() + ":" +
					 * parsedDataString);
					 */
				}
			} else {
				translatedTextView.setText("Translate failed!");
			}
		}
 
		public String getRawData(String string) {
			String dataString = null;
			// String queryString = "hello%20world&langpair=en%7Czh-CN";
			String queryString = string;
			try {
				URL url;
				if (bAtOffice) {
					url = new URL("http://10.85.40.153:8000/a.xml");
				} else {
					url = new URL(
							"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
									+ queryString);
				}
 
				URLConnection conn = url.openConnection();
				conn.connect();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(conn.getInputStream()));
 
				byte buffer[] = new byte[1024];
 
				// bis.r
				String readerString;
				while ((readerString = reader.readLine()) != null) {
					dataString += readerString;
				}
 
				reader.close();
				// is.close();
 
			} catch (IOException e) {
				System.out.print("Net work error");
			}
			return dataString;
		}
	}

可能相关


4 条评论 »

  1. pjq 说道:

    呵呵,不错,继续努力。

  2. pjq 说道:

    很不错,继续努力。

  3. pjq 说道:

    已经更新源码了:
    详见https://github.com/pjq/GTranslator/tree,
    谢谢你的建议。

发表评论

电子邮件地址不会被公开。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">