??? 一種中英文翻譯工具靈格斯,原理如下,輸入相關的要翻譯的字詞,到相關網站中查詢,將結果在本地顯示。手機中實現這個功能,必須用手機訪問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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

