一种中英文翻译工具灵格斯,原理如下,输入相关的要翻译的字词,到相关网站中查询,将结果在本地显示。手机中实现这个功能,必须用手机访问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(/blog_article/urlPath/index.html); 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; } }
效果图如下:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
}
我的布局里面有很多个textView 我总不能一个个重新设置 但是他们有 很多共性
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int s1=0x7f050000;
public static final int s10=0x7f050009;
public static final int s11=0x7f05000a;
public static final int s12=0x7f05000b;
public static final int s13=0x7f05000c;
public static final int s14=0x7f05000d;
public static final int s15=0x7f05000e;
public static final int s16=0x7f05000f;
public static final int s2=0x7f050001;
public static final int s3=0x7f050002;
public static final int s4=0x7f050003;
public static final int s5=0x7f050004;
public static final int s6=0x7f050005;
public static final int s7=0x7f050006;
public static final int s8=0x7f050007;
public static final int s9=0x7f050008;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int toast=0x7f030001;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int s2=0x7f040001;
}
}
上面的是我的r文件
import java.lang.reflect.Field;
/* ... */
for (int i = 1; i < 16; i++) {
int id = R.id.class.getField("s" + i).getInt(0);
tv[i] = (TextView)findViewById(id);
tv[i].setTypeface(face);
tv[i].setClickable(true);
tv[i].setOnClickListener(clickListener);
}
当然如果你的布局如果只有textView组成 而没有其他的控件那就无需使用这个了
可以使用
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout01);
for (int i = 0; i < ll.getChildCount(); i++) {
((TextView) ll.getChildAt(i)).setText("Text View " + i);
}