??? 一種中英文翻譯工具靈格斯,原理如下,輸入相關(guān)的要翻譯的字詞,到相關(guān)網(wǎng)站中查詢,將結(jié)果在本地顯示。手機(jī)中實(shí)現(xiàn)這個(gè)功能,必須用手機(jī)訪問Web的知識(shí)。學(xué)習(xí)JEE的童鞋明白,許多東西底層使用Apache HttpClient實(shí)現(xiàn)功能。如一個(gè)XFire底層訪問,一些web的服務(wù)器底層等,一些常用的應(yīng)用程序訪問web網(wǎng)站等都是用這個(gè)組件開發(fā),學(xué)習(xí)Android的童鞋會(huì)發(fā)現(xiàn)Android SDK中包含這個(gè)組件HttpClient, 但是他的功能沒有JEE的HTTPClient的公共強(qiáng)大,但是仍然非常強(qiáng)悍!好了言歸正傳,開始講解關(guān)于一個(gè)簡(jiǎn)單中英文翻譯字典的實(shí)現(xiàn)。
?
???? Android中實(shí)現(xiàn)原理講解:采用HttpClient或者URLConnection訪問得到結(jié)果,解析實(shí)現(xiàn)而已。至于界面嗎?可以自行安排。好了看一下實(shí)現(xiàn)效果唄!
?
重點(diǎn)代碼如下:
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; /** * 手機(jī)訪問遠(yuǎn)程http請(qǐng)求的信息 * @author longgangbai * @date 2010-5-25 * @version 1.0 * @since JDK6.0 */ public class HTTPClient { private final static String DEFAULT_CHARSET="UTF-8"; /** * 手機(jī)遠(yuǎn)程請(qǐng)求文件信息解析 * @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(); //設(shè)置請(qǐng)求的方式 httpCon.setRequestMethod("POST"); //設(shè)置輸入的信息 httpCon.setDoInput(true); //設(shè)置輸出信息 httpCon.setDoOutput(true); //設(shè)置是否使用緩存 httpCon.setUseCaches(false); //設(shè)置請(qǐng)求的參數(shù) 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發(fā)送xml數(shù)據(jù),使用is接收服務(wù)端返回的數(shù)據(jù) result=getResponseResult(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { is.close(); os.close(); } catch (Exception e) { } } return result; } /** * 獲取相應(yīng)的信息 * @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; } /** * 手機(jī)訪問頁(yè)面采用apache的訪問 * @param httpurl * @return */ public static String executeHttpClientByApache(String httpurl,Map<String, String> map) { // 構(gòu)建HttpClient的實(shí)例的應(yīng)用 HttpClient httpclient=new DefaultHttpClient(); //設(shè)置post發(fā)送的對(duì)象 HttpPost httpPost = new HttpPost(); //設(shè)置各種請(qǐng)求的頭部信息和參數(shù) 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 { //設(shè)置請(qǐng)求的格式為UTF-8形式 httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //獲取響應(yīng)的信息 HttpResponse response= httpclient.execute(httpPost); //獲取響應(yīng)的狀態(tài) 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; } }
?
效果圖如下:
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
