六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

一个简单的中英文翻译词典学习(类似灵格斯)一

[复制链接]

升级  73.8%

273

主题

273

主题

273

主题

进士

Rank: 4

积分
869
 楼主| 发表于 2013-1-15 02:29:15 | 显示全部楼层 |阅读模式
    一种中英文翻译工具灵格斯,原理如下,输入相关的要翻译的字词,到相关网站中查询,将结果在本地显示。手机中实现这个功能,必须用手机访问Web的知识。学习JEE的童鞋明白,许多东西底层使用Apache HttpClient实现功能。如一个XFire底层访问,一些web的服务器底层等,一些常用的应用程序访问web网站等都是用这个组件开发,学习Android的童鞋会发现Android SDK中包含这个组件HttpClient, 但是他的功能没有JEE的HTTPClient的公共强大,但是仍然非常强悍!好了言归正传,开始讲解关于一个简单中英文翻译字典的实现。
 
     Android中实现原理讲解:采用HttpClient或者URLConnection访问得到结果,解析实现而已。至于界面吗?可以自行安排。好了看一下实现效果呗!
 
重点代码如下:
package com.easyway.android.xdict;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.Map.Entry;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;/** * 手机访问远程http请求的信息 * @author longgangbai * @date 2010-5-25 * @version 1.0 * @since JDK6.0 */public class HTTPClient {private final static  String DEFAULT_CHARSET="UTF-8";/** * 手机远程请求文件信息解析 * @param urlPath * @param map * @return */public  static String executeByHttpURLConnection(String urlPath,Map<String, String> map){String result="";InputStream is =null;OutputStream os = null;try {URL url = new URL(urlPath);HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();//设置请求的方式httpCon.setRequestMethod("POST");//设置输入的信息httpCon.setDoInput(true);//设置输出信息httpCon.setDoOutput(true);//设置是否使用缓存httpCon.setUseCaches(false);//设置请求的参数if(map!=null){Set<Entry<String,String>>  entryMaps=map.entrySet();for (Entry<String, String> entry : entryMaps) {httpCon.addRequestProperty(entry.getKey(), entry.getValue());}}httpCon.setRequestProperty("Charset", DEFAULT_CHARSET);is = httpCon.getInputStream();//os = httpCon.getOutputStream();//  使用os发送xml数据,使用is接收服务端返回的数据    result=getResponseResult(is);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try { is.close(); os.close();} catch (Exception e) {}}return result;}/** * 获取相应的信息 * @param is * @return * @throws IOException */public static String getResponseResult(InputStream is ) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(is, DEFAULT_CHARSET)); String line=null; String result=""; while((line=br.readLine())!=null){ result+=line; }return result;} /**  * 手机访问页面采用apache的访问  * @param httpurl  * @return  */ public static String executeHttpClientByApache(String httpurl,Map<String, String> map) { // 构建HttpClient的实例的应用 HttpClient httpclient=new DefaultHttpClient(); //设置post发送的对象 HttpPost httpPost = new HttpPost(); //设置各种请求的头部信息和参数 List<NameValuePair> params=new ArrayList<NameValuePair>(); if(map!=null){Set<Entry<String,String>>  entryMaps=map.entrySet();for (Entry<String, String> entry : entryMaps) {params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));} } String result=""; try { //设置请求的格式为UTF-8形式  httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));  //获取响应的信息  HttpResponse response= httpclient.execute(httpPost);  //获取响应的状态  int statusCode=response.getStatusLine().getStatusCode();  if(statusCode==200){  result=EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);  }else{  System.out.println("statusCode="+statusCode);  }} catch (UnsupportedEncodingException e) {e.printStackTrace();}catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}    return result; }} 
效果图如下:
 
 

 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表