J2ME显示google天气预报
2010-08-05 08:52
330 查看
这个小程序写了好久了,但是前一阵一直因为三星手机无法显示图片的问题无法解决。现在那个问题解决了,不是三星的问题,是要过滤移动资费页面。这个程序也能贴出来了。
通过高级UI编写,要用到kxml2来解析google服务器的返回值。显示部分是Form和CustomItem.只显示日期,未来几天的气温以及表示天气状况的图片,其他很多信息没有显示,但是在所有获取的信息都已解析出来并在控制台打印,需要的话可以直接拿来再屏幕上显示。我在url里面吧城市设定为上海,可以直接改。
程序如下:
通过高级UI编写,要用到kxml2来解析google服务器的返回值。显示部分是Form和CustomItem.只显示日期,未来几天的气温以及表示天气状况的图片,其他很多信息没有显示,但是在所有获取的信息都已解析出来并在控制台打印,需要的话可以直接拿来再屏幕上显示。我在url里面吧城市设定为上海,可以直接改。
程序如下:
package googleweather; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.lcdui.CustomItem; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import org.kxml2.io.KXmlParser; import org.xmlpull.v1.XmlPullParserException; public class GoogleWeather extends MIDlet { InputStream is; Display dis; Form f; int width ; String city ; String forecast_date; public GoogleWeather() { dis = Display.getDisplay(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { f = new Form("天气预报"); width = f.getWidth(); dis.setCurrent(f); getIS() ; if (is == null) f.append("is == null"); parse(); } public boolean getIS() { String url = "http://www.google.com/ig/api?weather=Shanghai"; HttpConnection hcon = null; boolean ret = false; try { hcon = (HttpConnection) Connector.open(url); String contentType = hcon.getHeaderField("Content-Type"); // 这里用来过滤移动资费页面 if (contentType != null && contentType.indexOf("text/vnd.wap.wml") != -1) { hcon = null; hcon = (HttpConnection) Connector.open(url); } is = new DataInputStream(hcon.openInputStream()); ret = true; } catch (Exception e) { ret = false; e.printStackTrace(); } return ret; } public boolean parse() { KXmlParser parser; parser = new KXmlParser(); boolean ret = false; try { parser.setInput(new InputStreamReader(is)); xml_api_reply(parser); } catch (Exception e) { e.printStackTrace(); } try { is.close(); } catch (IOException e) { e.printStackTrace(); } return ret; } private void xml_api_reply(KXmlParser parser) { try { parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "xml_api_reply"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "weather"); parser.nextTag(); forecast_information(parser); parser.nextTag(); current_conditions(parser); parser.nextTag(); forecast_conditions(parser); parser.nextTag(); forecast_conditions(parser); parser.nextTag(); forecast_conditions(parser); parser.nextTag(); forecast_conditions(parser); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "weather"); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "xml_api_reply"); } catch (XmlPullParserException e) { f.append("11"); } catch (IOException e) { f.append("12"); } } private void current_conditions(KXmlParser parser) throws XmlPullParserException, IOException { String condition,temp_f,temp_c ,humidity ,icon,wind_condition ; parser.require(KXmlParser.START_TAG, null, "current_conditions"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "condition"); condition = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "condition"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "temp_f"); temp_f = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "temp_f"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "temp_c"); temp_c = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "temp_c"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "humidity"); humidity = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "humidity"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "icon"); icon = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "icon"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "wind_condition"); wind_condition = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "wind_condition"); parser.nextTag(); System.out.println(condition); System.out.println(temp_f); System.out.println(temp_c); System.out.println(humidity); System.out.println(icon); System.out.println(wind_condition); icon = "http://www.google.com" + icon ; CurrentWeatherItem cwi = new CurrentWeatherItem(temp_c , city , forecast_date, loadHttpFile(icon)); f.append(cwi); parser.require(KXmlParser.END_TAG, null, "current_conditions"); } private Image loadHttpFile(String url) throws IOException { System.out.println(url); byte[] byteBuffer = null; HttpConnection hc = (HttpConnection) Connector.open(url); String contentType = hc.getHeaderField("Content-Type"); // 这里用来过滤移动资费页面 if (contentType != null && contentType.indexOf("text/vnd.wap.wml") != -1) { hc = null; hc = (HttpConnection) Connector.open(url); } int code = hc.getResponseCode(); if (code == 200) { try { InputStream is = hc.openInputStream(); try { int len = (int) hc.getLength(); if (len > 0) { byteBuffer = new byte[len]; int done = 0; while (done < len) { done += is.read(byteBuffer, done, len - done); } } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int count; while ((count = is.read(buffer)) >= 0) { bos.write(buffer, 0, count); } byteBuffer = bos.toByteArray(); } } finally { is.close(); } } finally { hc.close(); } } return Image.createImage(byteBuffer, 0, byteBuffer.length); } private void forecast_information(KXmlParser parser) throws XmlPullParserException, IOException { String city,postal_code,latitude_e6,longitude_e6,forecast_date,current_date_time,unit_system; parser.require(KXmlParser.START_TAG, null, "forecast_information"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "city"); city = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "city"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "postal_code"); postal_code = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "postal_code"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "latitude_e6"); latitude_e6 = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "latitude_e6"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "longitude_e6"); longitude_e6 = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "longitude_e6"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "forecast_date"); forecast_date = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "forecast_date"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "current_date_time"); current_date_time = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "current_date_time"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "unit_system"); unit_system = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "unit_system"); parser.nextTag(); System.out.println(city); System.out.println(postal_code); System.out.println(latitude_e6); System.out.println(longitude_e6); System.out.println(forecast_date); System.out.println(current_date_time); System.out.println(unit_system); this.city = city ; this.forecast_date = forecast_date ; parser.require(KXmlParser.END_TAG, null, "forecast_information"); } private void forecast_conditions(KXmlParser parser) throws XmlPullParserException, IOException { String day_of_week,low,high,icon ,condition ; parser.require(KXmlParser.START_TAG, null, "forecast_conditions"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "day_of_week"); day_of_week = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "day_of_week"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "low"); low = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "low"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "high"); high = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "high"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "icon"); icon = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "icon"); parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "condition"); condition = parser.getAttributeValue(0); parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "condition"); parser.nextTag(); System.out.println(day_of_week); System.out.println(low); System.out.println(high); System.out.println(icon); System.out.println(condition); icon = "http://www.google.com" + icon ; if(day_of_week.equalsIgnoreCase("mon")) day_of_week = "周一" ; else if(day_of_week.equalsIgnoreCase("tue")) day_of_week = "周二"; else if(day_of_week.equalsIgnoreCase("wed")) day_of_week = "周三"; else if(day_of_week.equalsIgnoreCase("thu")) day_of_week = "周四"; else if(day_of_week.equalsIgnoreCase("fri")) day_of_week = "周五"; else if(day_of_week.equalsIgnoreCase("sat")) day_of_week = "周六"; else day_of_week = "周日"; low = String.valueOf((Integer.parseInt(low) - 32) * 5 / 9); high = String.valueOf((Integer.parseInt(high) - 32) * 5 / 9); ForecastWeatherItem fwi = new ForecastWeatherItem(day_of_week, low , high , loadHttpFile(icon)); f.append(fwi); parser.require(KXmlParser.END_TAG, null, "forecast_conditions"); } public class CurrentWeatherItem extends CustomItem{ String temp ; String city ; String forecast_date; Image icon; protected CurrentWeatherItem(String temp , String city ,String forecast_date,Image icon ) { super(forecast_date); this.temp = temp ; this.city = city; this.forecast_date = forecast_date; this.icon = icon; } protected int getMinContentHeight() { // TODO Auto-generated method stub return 40; } protected int getMinContentWidth() { // TODO Auto-generated method stub return width; } protected int getPrefContentHeight(int arg0) { // TODO Auto-generated method stub return 40; } protected int getPrefContentWidth(int arg0) { // TODO Auto-generated method stub return width; } protected void paint(Graphics g, int arg1, int arg2) { g.setColor(0x9accff); g.fillRect(0, 0, width, 40); g.drawImage(this.icon, 20, 20, Graphics.HCENTER | Graphics.VCENTER); g.setColor(0x000000); g.drawString("当前气温:" , 50, 10, 0); g.drawString(temp + "℃" , 100, 10, 0); } } public class ForecastWeatherItem extends CustomItem{ String day_of_week; String low; String high ; Image icon; protected ForecastWeatherItem(String day_of_week, String low, String high , Image icon) { super(null); this.day_of_week = day_of_week; this.low = low; this.high = high ; this.icon = icon ; } protected int getMinContentHeight() { // TODO Auto-generated method stub return 40; } protected int getMinContentWidth() { // TODO Auto-generated method stub return width; } protected int getPrefContentHeight(int arg0) { // TODO Auto-generated method stub return 40; } protected int getPrefContentWidth(int arg0) { // TODO Auto-generated method stub return width; } protected void paint(Graphics g, int arg1, int arg2) { g.setColor(0x66cbff); g.fillRect(0, 0, width, 40); g.drawImage(this.icon, 20, 20, Graphics.HCENTER | Graphics.VCENTER); g.setColor(0x000000); g.drawString(day_of_week , 50, 10, 0); g.drawString(low + "℃ /" + high + "℃", 100, 10, 0); } } }
相关文章推荐
- java获取google的简单天气预报
- Vista天气预报--修正不显示中国天气预报的BUG
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 打造你的个性:在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- Office2010--如何在OUTLOOK今日显示一周天气预报-视频
- 调用265天气预报,不显示链接
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态、中国农历
- 调用265天气预报,不显示链接
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 显示天气预报、日历、时钟、MSN、QQ在线状态
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态
- 天气预报的一个窗体的显示!
- 使用Google Weather API查询天气预报
- axis WebServices 完美调用天气预报,查询、显示 代码!
- 正则表达式应用笔记----解析网页,获取并显示“未来三天全国天气预报”(java)
- 根据IP显示天气预报代码:只显示成一行,可在页面调用JS即可
- 在blog中显示天气预报、日历、时钟、MSN、QQ在线状态(转载)