实习比较忙,也时间写了,就记录一下自己的代码成长吧,希望能帮到你。
MainActivity部分:
package com.sbs.weather; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import com.sbs.weather.DownImage.ImageCallback; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.NetworkInfo.State; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.ContactsContract.CommonDataKinds.Note; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class Weather extends Activity { public EditText ETplace; public TextView TvPlace; public TextView TvPlace_weather_tomrrow; public TextView TvPlace_weather_next; public TextView TvPlace_weather_detail; public Button query; public TextView placeName; public ImageView imView_today1; public ImageView imView_today2; public ImageView imView_tomrrow1; public ImageView imView_tomrrow2; public ImageView imView_next1; public ImageView imView_next2; String place; // 加载提示框 private ProgressDialog dialog = null; private final int IS_FINISH = 1; private Handler handler = new Handler() { public void handleMessage(Message msg) { String weather = ""; String weather_tomrrow = ""; String weather_next = ""; String weather_detail = ""; NodeList n = (NodeList) msg.obj; // NodeList n = new NodeList(); // String strResult = // EntityUtils.toString(resp.getEntity()); // Log.i("weather->", strResult); // 一华氏度等于9/5摄氏度数值+32 String henan = n.item(0).getChildNodes().item(0).getNodeValue(); Log.i("省会", henan); String zhengzhou_1 = n.item(1).getChildNodes().item(0) .getNodeValue(); Log.i("市区", zhengzhou_1); String time_4 = n.item(4).getChildNodes().item(0).getNodeValue(); Log.i("时间", time_4); String wendu_5 = n.item(5).getChildNodes().item(0).getNodeValue(); Log.i("温度", wendu_5); String duoyun_6 = n.item(6).getChildNodes().item(0).getNodeValue(); Log.i("日期", duoyun_6); String weifeng_7 = n.item(7).getChildNodes().item(0).getNodeValue(); Log.i("风速", weifeng_7); String picture1_8 = n.item(8).getChildNodes().item(0) .getNodeValue(); Log.i("图片1", picture1_8); String picture2_9 = n.item(9).getChildNodes().item(0) .getNodeValue(); Log.i("图片2", picture2_9); String content_10 = n.item(10).getChildNodes().item(0) .getNodeValue(); Log.i("今天天气描述", content_10); String content2_11 = n.item(11).getChildNodes().item(0) .getNodeValue(); Log.i("天气小贴士", content2_11); String tomrrow_12 = n.item(12).getChildNodes().item(0) .getNodeValue(); Log.i("明天气温", tomrrow_12); String tomrrow_13 = n.item(13).getChildNodes().item(0) .getNodeValue(); Log.i("明天概况", tomrrow_13); String tomrrow_14 = n.item(14).getChildNodes().item(0) .getNodeValue(); Log.i("明天风向和风力", tomrrow_14); String tomrrow_15 = n.item(15).getChildNodes().item(0) .getNodeValue(); Log.i("明天图片1", tomrrow_15); String tomrrow_16 = n.item(16).getChildNodes().item(0) .getNodeValue(); Log.i("明天图片2", tomrrow_16); String tomrrow2_17 = n.item(17).getChildNodes().item(0) .getNodeValue(); Log.i("后天气温", tomrrow2_17); String tomrrow2_18 = n.item(18).getChildNodes().item(0) .getNodeValue(); Log.i("后天概况", tomrrow2_18); String tomrrow2_19 = n.item(19).getChildNodes().item(0) .getNodeValue(); Log.i("后天风向和风力", tomrrow2_19); String tomrrow2_20 = n.item(20).getChildNodes().item(0) .getNodeValue(); Log.i("后天图片1", tomrrow2_20); String tomrrow2_21 = n.item(21).getChildNodes().item(0) .getNodeValue(); Log.i("后天图片2", tomrrow2_21); String zonghe_22 = n.item(22).getChildNodes().item(0) .getNodeValue(); Log.i("介绍 ", zonghe_22); // 获得图片url 当天的。 String imgUrl = "http://www.webxml.com.cn/images/weather/"; String TodayimgUrl1 = null; TodayimgUrl1 = imgUrl + n.item(8).getChildNodes().item(0).getNodeValue(); Log.i("今天的图片1地址", TodayimgUrl1); String TodayimgUrl2 = null; TodayimgUrl2 = imgUrl + n.item(9).getChildNodes().item(0).getNodeValue(); Log.i("今天的图片2地址", TodayimgUrl2); // 今天图片异步加载部分 // String imrUrl = data.getJSONObject(0).get("imrUrl").toString(); DownImage loadimage_today_one = new DownImage(TodayimgUrl1); loadimage_today_one.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_today1.setImageDrawable(drawable); } }); DownImage loadimage_today_two = new DownImage(TodayimgUrl2); loadimage_today_two.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_today2.setImageDrawable(drawable); } }); // imView_today1.setImageBitmap(Utils.returnBitMap(TodayimgUrl1)); // imView_today2.setImageBitmap(Utils.returnBitMap(TodayimgUrl2)); // 获得图片url 明天的。 String TomorrowimgUrl1 = null; TomorrowimgUrl1 = imgUrl + n.item(8).getChildNodes().item(0).getNodeValue(); Log.i("明天的图片1地址", TomorrowimgUrl1); String TomorrowimgUrl2 = null; TomorrowimgUrl2 = imgUrl + n.item(9).getChildNodes().item(0).getNodeValue(); Log.i("明天的图片2地址", TomorrowimgUrl1); DownImage loadimage_tomrrow_one = new DownImage(TomorrowimgUrl1); loadimage_tomrrow_one.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_tomrrow1.setImageDrawable(drawable); } }); DownImage loadimage_tomrrow_two = new DownImage(TomorrowimgUrl2); loadimage_tomrrow_two.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_tomrrow2.setImageDrawable(drawable); } }); // imView_tomrrow1.setImageBitmap(Utils.returnBitMap(TomorrowimgUrl1)); // imView_tomrrow2.setImageBitmap(Utils.returnBitMap(TomorrowimgUrl2)); // 获得图片url 后天的。 String NextimgUrl1 = null; NextimgUrl1 = imgUrl + n.item(8).getChildNodes().item(0).getNodeValue(); Log.i("后天的图片1地址", NextimgUrl1); String NextimgUrl2 = null; NextimgUrl2 = imgUrl + n.item(9).getChildNodes().item(0).getNodeValue(); Log.i("后天的图片2地址", NextimgUrl2); DownImage loadimage_next_one = new DownImage(NextimgUrl1); loadimage_next_one.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_next1.setImageDrawable(drawable); } }); DownImage loadimage_next_two = new DownImage(NextimgUrl2); loadimage_next_two.loadImage(new ImageCallback() { @Override public void getDrawable(Drawable drawable) { // TODO Auto-generated method stub imView_next2.setImageDrawable(drawable); } }); // imView_next1.setImageBitmap(Utils.returnBitMap(NextimgUrl1)); // imView_next2.setImageBitmap(Utils.returnBitMap(NextimgUrl2)); // 今天天气 weather += henan; weather += "/"; weather += zhengzhou_1; weather += "\n"; weather += time_4; weather += "\n"; weather += wendu_5; weather += "\n"; weather += duoyun_6; weather += "\n"; weather += weifeng_7; weather += "\n"; weather += content_10; weather += "\n"; weather += content2_11; weather += "\n"; // 明天天气 weather_tomrrow += tomrrow_12; weather_tomrrow += "\n"; weather_tomrrow += tomrrow_13; weather_tomrrow += "\n"; weather_tomrrow += tomrrow_14; weather_tomrrow += "\n"; // 后天天气 weather_next += tomrrow2_17; weather_next += "\n"; weather_next += tomrrow2_18; weather_next += "\n"; weather_next += tomrrow2_19; weather_next += "\n"; // 地区介绍 weather_detail += zonghe_22; weather_detail += "\n"; TvPlace.setText(weather); Log.i("parseed weather->", weather); TvPlace_weather_tomrrow.setText(weather_tomrrow); Log.i("parseed weather_tomrrow->", weather_tomrrow); TvPlace_weather_next.setText(weather_next); Log.i("parseed weather_next->", weather_next); TvPlace_weather_detail.setText(weather_detail); Log.i("parseed weather_detail->", weather_detail); if (msg.what == IS_FINISH) { dialog.dismiss(); } }; }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ETplace = (EditText) findViewById(R.id.place1); query = (Button) findViewById(R.id.query); imView_today1 = (ImageView) findViewById(R.id.myImageView_today1); imView_today2 = (ImageView) findViewById(R.id.myImageView_today2); imView_tomrrow1 = (ImageView) findViewById(R.id.myImageView_tomrrow1); imView_tomrrow2 = (ImageView) findViewById(R.id.myImageView_tomrrow2); imView_next1 = (ImageView) findViewById(R.id.myImageView_next1); imView_next2 = (ImageView) findViewById(R.id.myImageView_next2); TvPlace = (TextView) findViewById(R.id.tvPlace); TvPlace_weather_tomrrow = (TextView) findViewById(R.id.tvPlace_weather_tomrrow); TvPlace_weather_next = (TextView) findViewById(R.id.tvPlace_weather_next); TvPlace_weather_detail = (TextView) findViewById(R.id.tvPlace_weather_detail); placeName = (TextView) findViewById(R.id.placeName); query.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { new Thread(new MyThread()).start(); dialog.show(); } }); // 加载提示框 dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMessage("正在下载,请稍后...."); dialog.setCancelable(false); } public class MyThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub try { place = CntoEn.getFullSpell(ETplace.getText().toString()); Log.i("Message->", place); placeName.setText(place); String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=" + place; Log.i("url->", url); DefaultHttpClient client = new DefaultHttpClient(); HttpUriRequest req = new HttpGet(url); HttpResponse resp = client.execute(req); HttpEntity ent = resp.getEntity(); InputStream stream = ent.getContent(); DocumentBuilder b = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); Document d = b.parse(new InputSource(stream)); NodeList n = d.getElementsByTagName("string"); Message message = Message.obtain(); message.obj = n; message.what = IS_FINISH; handler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } } }
图片解析公用类:
package com.sbs.weather; import java.net.URL; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; public class DownImage { private String image_path; public DownImage(String image_path) { // TODO Auto-generated constructor stub this.image_path = image_path; } public void loadImage(final ImageCallback callback) { final Handler handler = new Handler() { @Override public void handleMessage(android.os.Message msg) { super.handleMessage(msg); callback.getDrawable((Drawable) msg.obj); }; }; new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Drawable drawable = Drawable.createFromStream(new URL( image_path).openStream(), ""); // callback.getDrawable(drawable); Message message = Message.obtain(); message.obj = drawable; handler.sendMessage(message); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }).start(); } // 接口回调方式 public interface ImageCallback { public void getDrawable(Drawable drawable); } }
汉字转拼音公用类:
package com.sbs.weather; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; /** * 汉字转化为全拼 * * JDK版本: 1.4 */ public class CntoEn { private static LinkedHashMap spellMap = null; static { if (spellMap == null) { spellMap = new LinkedHashMap(400); } initialize(); System.out.println("Chinese transfer Spell Done."); } private static void spellPut(String spell, int ascii) { spellMap.put(spell, new Integer(ascii)); } private static void initialize() { spellPut("a", -20319); spellPut("ai", -20317); spellPut("an", -20304); spellPut("ang", -20295); spellPut("ao", -20292); spellPut("ba", -20283); spellPut("bai", -20265); spellPut("ban", -20257); spellPut("bang", -20242); spellPut("bao", -20230); spellPut("bei", -20051); spellPut("ben", -20036); spellPut("beng", -20032); spellPut("bi", -20026); spellPut("bian", -20002); spellPut("biao", -19990); spellPut("bie", -19986); spellPut("bin", -19982); spellPut("bing", -19976); spellPut("bo", -19805); spellPut("bu", -19784); spellPut("ca", -19775); spellPut("cai", -19774); spellPut("can", -19763); spellPut("cang", -19756); spellPut("cao", -19751); spellPut("ce", -19746); spellPut("ceng", -19741); spellPut("cha", -19739); spellPut("chai", -19728); spellPut("chan", -19725); spellPut("chang", -19715); spellPut("chao", -19540); spellPut("che", -19531); spellPut("chen", -19525); spellPut("cheng", -19515); spellPut("chi", -19500); spellPut("chong", -19484); spellPut("chou", -19479); spellPut("chu", -19467); spellPut("chuai", -19289); spellPut("chuan", -19288); spellPut("chuang", -19281); spellPut("chui", -19275); spellPut("chun", -19270); spellPut("chuo", -19263); spellPut("ci", -19261); spellPut("cong", -19249); spellPut("cou", -19243); spellPut("cu", -19242); spellPut("cuan", -19238); spellPut("cui", -19235); spellPut("cun", -19227); spellPut("cuo", -19224); spellPut("da", -19218); spellPut("dai", -19212); spellPut("dan", -19038); spellPut("dang", -19023); spellPut("dao", -19018); spellPut("de", -19006); spellPut("deng", -19003); spellPut("di", -18996); spellPut("dian", -18977); spellPut("diao", -18961); spellPut("die", -18952); spellPut("ding", -18783); spellPut("diu", -18774); spellPut("dong", -18773); spellPut("dou", -18763); spellPut("du", -18756); spellPut("duan", -18741); spellPut("dui", -18735); spellPut("dun", -18731); spellPut("duo", -18722); spellPut("e", -18710); spellPut("en", -18697); spellPut("er", -18696); spellPut("fa", -18526); spellPut("fan", -18518); spellPut("fang", -18501); spellPut("fei", -18490); spellPut("fen", -18478); spellPut("feng", -18463); spellPut("fo", -18448); spellPut("fou", -18447); spellPut("fu", -18446); spellPut("ga", -18239); spellPut("gai", -18237); spellPut("gan", -18231); spellPut("gang", -18220); spellPut("gao", -18211); spellPut("ge", -18201); spellPut("gei", -18184); spellPut("gen", -18183); spellPut("geng", -18181); spellPut("gong", -18012); spellPut("gou", -17997); spellPut("gu", -17988); spellPut("gua", -17970); spellPut("guai", -17964); spellPut("guan", -17961); spellPut("guang", -17950); spellPut("gui", -17947); spellPut("gun", -17931); spellPut("guo", -17928); spellPut("ha", -17922); spellPut("hai", -17759); spellPut("han", -17752); spellPut("hang", -17733); spellPut("hao", -17730); spellPut("he", -17721); spellPut("hei", -17703); spellPut("hen", -17701); spellPut("heng", -17697); spellPut("hong", -17692); spellPut("hou", -17683); spellPut("hu", -17676); spellPut("hua", -17496); spellPut("huai", -17487); spellPut("huan", -17482); spellPut("huang", -17468); spellPut("hui", -17454); spellPut("hun", -17433); spellPut("huo", -17427); spellPut("ji", -17417); spellPut("jia", -17202); spellPut("jian", -17185); spellPut("jiang", -16983); spellPut("jiao", -16970); spellPut("jie", -16942); spellPut("jin", -16915); spellPut("jing", -16733); spellPut("jiong", -16708); spellPut("jiu", -16706); spellPut("ju", -16689); spellPut("juan", -16664); spellPut("jue", -16657); spellPut("jun", -16647); spellPut("ka", -16474); spellPut("kai", -16470); spellPut("kan", -16465); spellPut("kang", -16459); spellPut("kao", -16452); spellPut("ke", -16448); spellPut("ken", -16433); spellPut("keng", -16429); spellPut("kong", -16427); spellPut("kou", -16423); spellPut("ku", -16419); spellPut("kua", -16412); spellPut("kuai", -16407); spellPut("kuan", -16403); spellPut("kuang", -16401); spellPut("kui", -16393); spellPut("kun", -16220); spellPut("kuo", -16216); spellPut("la", -16212); spellPut("lai", -16205); spellPut("lan", -16202); spellPut("lang", -16187); spellPut("lao", -16180); spellPut("le", -16171); spellPut("lei", -16169); spellPut("leng", -16158); spellPut("li", -16155); spellPut("lia", -15959); spellPut("lian", -15958); spellPut("liang", -15944); spellPut("liao", -15933); spellPut("lie", -15920); spellPut("lin", -15915); spellPut("ling", -15903); spellPut("liu", -15889); spellPut("long", -15878); spellPut("lou", -15707); spellPut("lu", -15701); spellPut("lv", -15681); spellPut("luan", -15667); spellPut("lue", -15661); spellPut("lun", -15659); spellPut("luo", -15652); spellPut("ma", -15640); spellPut("mai", -15631); spellPut("man", -15625); spellPut("mang", -15454); spellPut("mao", -15448); spellPut("me", -15436); spellPut("mei", -15435); spellPut("men", -15419); spellPut("meng", -15416); spellPut("mi", -15408); spellPut("mian", -15394); spellPut("miao", -15385); spellPut("mie", -15377); spellPut("min", -15375); spellPut("ming", -15369); spellPut("miu", -15363); spellPut("mo", -15362); spellPut("mou", -15183); spellPut("mu", -15180); spellPut("na", -15165); spellPut("nai", -15158); spellPut("nan", -15153); spellPut("nang", -15150); spellPut("nao", -15149); spellPut("ne", -15144); spellPut("nei", -15143); spellPut("nen", -15141); spellPut("neng", -15140); spellPut("ni", -15139); spellPut("nian", -15128); spellPut("niang", -15121); spellPut("niao", -15119); spellPut("nie", -15117); spellPut("nin", -15110); spellPut("ning", -15109); spellPut("niu", -14941); spellPut("nong", -14937); spellPut("nu", -14933); spellPut("nv", -14930); spellPut("nuan", -14929); spellPut("nue", -14928); spellPut("nuo", -14926); spellPut("o", -14922); spellPut("ou", -14921); spellPut("pa", -14914); spellPut("pai", -14908); spellPut("pan", -14902); spellPut("pang", -14894); spellPut("pao", -14889); spellPut("pei", -14882); spellPut("pen", -14873); spellPut("peng", -14871); spellPut("pi", -14857); spellPut("pian", -14678); spellPut("piao", -14674); spellPut("pie", -14670); spellPut("pin", -14668); spellPut("ping", -14663); spellPut("po", -14654); spellPut("pu", -14645); spellPut("qi", -14630); spellPut("qia", -14594); spellPut("qian", -14429); spellPut("qiang", -14407); spellPut("qiao", -14399); spellPut("qie", -14384); spellPut("qin", -14379); spellPut("qing", -14368); spellPut("qiong", -14355); spellPut("qiu", -14353); spellPut("qu", -14345); spellPut("quan", -14170); spellPut("que", -14159); spellPut("qun", -14151); spellPut("ran", -14149); spellPut("rang", -14145); spellPut("rao", -14140); spellPut("re", -14137); spellPut("ren", -14135); spellPut("reng", -14125); spellPut("ri", -14123); spellPut("rong", -14122); spellPut("rou", -14112); spellPut("ru", -14109); spellPut("ruan", -14099); spellPut("rui", -14097); spellPut("run", -14094); spellPut("ruo", -14092); spellPut("sa", -14090); spellPut("sai", -14087); spellPut("san", -14083); spellPut("sang", -13917); spellPut("sao", -13914); spellPut("se", -13910); spellPut("sen", -13907); spellPut("seng", -13906); spellPut("sha", -13905); spellPut("shai", -13896); spellPut("shan", -13894); spellPut("shang", -13878); spellPut("shao", -13870); spellPut("she", -13859); spellPut("shen", -13847); spellPut("sheng", -13831); spellPut("shi", -13658); spellPut("shou", -13611); spellPut("shu", -13601); spellPut("shua", -13406); spellPut("shuai", -13404); spellPut("shuan", -13400); spellPut("shuang", -13398); spellPut("shui", -13395); spellPut("shun", -13391); spellPut("shuo", -13387); spellPut("si", -13383); spellPut("song", -13367); spellPut("sou", -13359); spellPut("su", -13356); spellPut("suan", -13343); spellPut("sui", -13340); spellPut("sun", -13329); spellPut("suo", -13326); spellPut("ta", -13318); spellPut("tai", -13147); spellPut("tan", -13138); spellPut("tang", -13120); spellPut("tao", -13107); spellPut("te", -13096); spellPut("teng", -13095); spellPut("ti", -13091); spellPut("tian", -13076); spellPut("tiao", -13068); spellPut("tie", -13063); spellPut("ting", -13060); spellPut("tong", -12888); spellPut("tou", -12875); spellPut("tu", -12871); spellPut("tuan", -12860); spellPut("tui", -12858); spellPut("tun", -12852); spellPut("tuo", -12849); spellPut("wa", -12838); spellPut("wai", -12831); spellPut("wan", -12829); spellPut("wang", -12812); spellPut("wei", -12802); spellPut("wen", -12607); spellPut("weng", -12597); spellPut("wo", -12594); spellPut("wu", -12585); spellPut("xi", -12556); spellPut("xia", -12359); spellPut("xian", -12346); spellPut("xiang", -12320); spellPut("xiao", -12300); spellPut("xie", -12120); spellPut("xin", -12099); spellPut("xing", -12089); spellPut("xiong", -12074); spellPut("xiu", -12067); spellPut("xu", -12058); spellPut("xuan", -12039); spellPut("xue", -11867); spellPut("xun", -11861); spellPut("ya", -11847); spellPut("yan", -11831); spellPut("yang", -11798); spellPut("yao", -11781); spellPut("ye", -11604); spellPut("yi", -11589); spellPut("yin", -11536); spellPut("ying", -11358); spellPut("yo", -11340); spellPut("yong", -11339); spellPut("you", -11324); spellPut("yu", -11303); spellPut("yuan", -11097); spellPut("yue", -11077); spellPut("yun", -11067); spellPut("za", -11055); spellPut("zai", -11052); spellPut("zan", -11045); spellPut("zang", -11041); spellPut("zao", -11038); spellPut("ze", -11024); spellPut("zei", -11020); spellPut("zen", -11019); spellPut("zeng", -11018); spellPut("zha", -11014); spellPut("zhai", -10838); spellPut("zhan", -10832); spellPut("zhang", -10815); spellPut("zhao", -10800); spellPut("zhe", -10790); spellPut("zhen", -10780); spellPut("zheng", -10764); spellPut("zhi", -10587); spellPut("zhong", -10544); spellPut("zhou", -10533); spellPut("zhu", -10519); spellPut("zhua", -10331); spellPut("zhuai", -10329); spellPut("zhuan", -10328); spellPut("zhuang", -10322); spellPut("zhui", -10315); spellPut("zhun", -10309); spellPut("zhuo", -10307); spellPut("zi", -10296); spellPut("zong", -10281); spellPut("zou", -10274); spellPut("zu", -10270); spellPut("zuan", -10262); spellPut("zui", -10260); spellPut("zun", -10256); spellPut("zuo", -10254); } /** * 获得单个汉字的Ascii. * * @param cn * char 汉字字符 * @return int 错误返回 0,否则返回ascii */ public static int getCnAscii(char cn) { byte[] bytes = (String.valueOf(cn)).getBytes(); if (bytes == null || bytes.length > 2 || bytes.length <= 0) { // 错误 return 0; } if (bytes.length == 1) { // 英文字符 return bytes[0]; } if (bytes.length == 2) { // 中文字符 int hightByte = 256 + bytes[0]; int lowByte = 256 + bytes[1]; int ascii = (256 * hightByte + lowByte) - 256 * 256; // System.out.println("ASCII=" + ascii); return ascii; } return 0; // 错误 } /** * 根据ASCII码到SpellMap中查找对应的拼音 * * @param ascii * int 字符对应的ASCII * @return String 拼音,首先判断ASCII是否>0&<160,如果是返回对应的字符, * * 否则到SpellMap中查找,如果没有找到拼音,则返回null,如果找到则返回拼音. */ public static String getSpellByAscii(int ascii) { if (ascii > 0 && ascii < 160) { // 单字符 return String.valueOf((char) ascii); } if (ascii < -20319 || ascii > -10247) { // 不知道的字符 return null; } Set keySet = spellMap.keySet(); Iterator it = keySet.iterator(); String spell0 = null; ; String spell = null; int asciiRang0 = -20319; int asciiRang; while (it.hasNext()) { spell = (String) it.next(); Object valObj = spellMap.get(spell); if (valObj instanceof Integer) { asciiRang = ((Integer) valObj).intValue(); if (ascii >= asciiRang0 && ascii < asciiRang) { // 区间找到 return (spell0 == null) ? spell : spell0; } else { spell0 = spell; asciiRang0 = asciiRang; } } } return null; } /** * 返回字符串的全拼,是汉字转化为全拼,其它字符不进行转换 * * @param cnStr * String 字符串 * @return String 转换成全拼后的字符串 */ public static String getFullSpell(String cnStr) { if (null == cnStr || "".equals(cnStr.trim())) { return cnStr; } char[] chars = cnStr.toCharArray(); StringBuffer retuBuf = new StringBuffer(); for (int i = 0, Len = chars.length; i < Len; i++) { int ascii = getCnAscii(chars[i]); if (ascii == 0) { // 取ascii时出错 retuBuf.append(chars[i]); } else { String spell = getSpellByAscii(ascii); if (spell == null) { retuBuf.append(chars[i]); } else { retuBuf.append(spell); } // end of if spell == null } // end of if ascii <= -20400 } // end of for return retuBuf.toString(); } public static String getFirstSpell(String cnStr) { return null; } }
天气相关信息处理公用类方法:
package com.sbs.weather; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Utils { public static String week(String enWeek) { if (enWeek.equals("Mon") || enWeek.equals("Monday")) return "星期一"; else if (enWeek.equals("Tue") || enWeek.equals("Tuesday")) return "星期二"; else if (enWeek.equals("Wed") || enWeek.equals("Wednesday")) return "星期三"; else if (enWeek.equals("Thu") || enWeek.equals("Thursday")) return "星期四"; else if (enWeek.equals("Fri") || enWeek.equals("Friday")) return "星期五"; else if (enWeek.equals("Sat") || enWeek.equals("Saturday")) return "星期六"; else if (enWeek.equals("Sun") || enWeek.equals("Sunday")) return "星期日"; return ""; } public static String weather(String enWeather) { if (enWeather.equals("Clear")) return "晴"; else if (enWeather.equals("Partly Sunny") || enWeather.equals("partly_cloudy")) return "多云"; else if (enWeather.equals("Chance of Rain")) return "晴转雨"; else if (enWeather.equals("storm")) return "暴雨"; else if (enWeather.equals("thunderstorm")) return "雷阵雨"; else if (enWeather.equals("fog")) return "大雾"; else if (enWeather.equals("haze")) return "有雾"; else if (enWeather.equals("rain")) return "雨"; else if (enWeather.equals("heavyrain")) return "大雨"; else if (enWeather.equals("lightrain")) return "小雨"; else if (enWeather.equals("heavyrain")) return "大雨"; else if (enWeather.equals("snow")) return "有雪"; // / 还需要补充。。。。 return ""; } public static Bitmap returnBitMap(String imgUrl) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub } }).start(); URL myImgUrl = null; Bitmap bitmap = null; try { myImgUrl = new URL(imgUrl); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myImgUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
main.xml部分:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/placeName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:visibility="invisible" /> <EditText android:id="@+id/place1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="输入城市名称(汉字)" /> <Button android:id="@+id/query" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查询" /> <!-- 内容部分 --> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 今天天气 --> <TextView android:id="@+id/placeName110" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="今天天气 " /> <RelativeLayout android:id="@+id/shangwu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/placeName110" > <TextView android:id="@+id/placeName11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/placeName11" android:text="上午:" /> <ImageView android:id="@+id/myImageView_today1" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName11" android:contentDescription="@string/a" /> <TextView android:id="@+id/placeName111" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/myImageView_today1" android:text="下午:" /> <ImageView android:id="@+id/myImageView_today2" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName111" android:contentDescription="@string/aa" /> </RelativeLayout> <TextView android:id="@+id/tvPlace" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/shangwu" /> <!-- 明天天气 --> <TextView android:id="@+id/placeName220" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvPlace" android:text="明天天气 " /> <RelativeLayout android:id="@+id/shangwu_tomrrow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/placeName220" > <TextView android:id="@+id/placeName22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上午:" /> <ImageView android:id="@+id/myImageView_tomrrow1" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName22" android:contentDescription="@string/b" /> <TextView android:id="@+id/placeName222" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/myImageView_tomrrow1" android:text="下午:" /> <ImageView android:id="@+id/myImageView_tomrrow2" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName222" android:contentDescription="@string/bb" /> </RelativeLayout> <TextView android:id="@+id/tvPlace_weather_tomrrow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/shangwu_tomrrow" /> <!-- 后天天气 --> <TextView android:id="@+id/placeName330" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvPlace_weather_tomrrow" android:text="后天天气 " /> <RelativeLayout android:id="@+id/shangwu_next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/placeName330" > <TextView android:id="@+id/placeName33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上午:" /> <ImageView android:id="@+id/myImageView_next1" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName33" android:contentDescription="@string/c" /> <TextView android:id="@+id/placeName333" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/myImageView_next1" android:text="下午:" /> <ImageView android:id="@+id/myImageView_next2" android:layout_width="30dip" android:layout_height="30dip" android:layout_toRightOf="@+id/placeName333" android:contentDescription="@string/cc" /> </RelativeLayout> <TextView android:id="@+id/tvPlace_weather_next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/shangwu_next" /> <!-- 地区介绍 --> <TextView android:id="@+id/placeName440" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/tvPlace_weather_next" android:text="地区介绍 " /> <RelativeLayout android:id="@+id/shangwu_detail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/placeName440" > <TextView android:id="@+id/tvPlace_weather_detail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lineSpacingMultiplier="1.0"/> </RelativeLayout> </RelativeLayout> </ScrollView> </LinearLayout>
AndroidMainfest.xml部分:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sbs.weather" android:versionCode="1" android:versionName="1.0" > <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:name=".Weather" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
附效果图:
总结:大家可以试试,一般天气用这种解析还是很不错的,如果是listview就还是json吧。