日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

一個簡單的中英文翻譯詞典學習(類似靈格斯)一

系統 2454 0

??? 一種中英文翻譯工具靈格斯,原理如下,輸入相關的要翻譯的字詞,到相關網站中查詢,將結果在本地顯示。手機中實現這個功能,必須用手機訪問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;

	 }
}


  

?

效果圖如下:

?

?

一個簡單的中英文翻譯詞典學習(類似靈格斯)一

?

一個簡單的中英文翻譯詞典學習(類似靈格斯)一


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 淅川县| 固安县| 通榆县| 和平区| 包头市| 隆昌县| 通城县| 伽师县| 铁岭市| 肃北| 常州市| 翁牛特旗| 德兴市| 宁波市| 璧山县| 桐梓县| 连江县| 雷波县| 普陀区| 平利县| 孟州市| 斗六市| 榆社县| 石城县| 新疆| 焦作市| 兰西县| 衡山县| 共和县| 方城县| 开封县| 龙里县| 昌宁县| 广元市| 天水市| 山丹县| 江油市| 隆子县| 云浮市| 荔浦县| 固镇县|