private synchronized static String doTranslate(String text, boolean flag) {
String result = "";
HttpClient client = null;
PostMethod post = null;
try {
client = new HttpClient();
post = new PostMethod(URL);
NameValuePair[] params = {
new NameValuePair("v", "1.0"),
new NameValuePair("q", text),
new NameValuePair("langpair", flag ? "zh-CN|en"
: "en|zh-CN") };
post.setRequestBody(params);
post.getParams().setContentCharset("UTF-8");
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
result = post.getResponseBodyAsString();
System.out.println("result="+result);
} else {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (post != null) {
post.releaseConnection();
post = null;
client = null;
}
}
return null;
}