您的位置:首页 > 编程语言

[BlackBerry代码] 简单的访问sina天气频道获取天气信息的例子

2008-05-15 17:48 459 查看
老规矩,只有代码。 /* * BBWeatherForm.java * * ? dreamfrank@SubZero Studio, 2007 */ import java.io.*;import javax.microedition.io.*;import java.lang.*;import javax.microedition.lcdui.*; /** * */class BBWeatherForm extends Form implements CommandListener { private BBWeather bbweather; private Command getCommand; private TextField txtCity; private TextField txtResult; private String[] strCity = {"", "", ""}; private String[] strDate = {"", "", ""}; private String[] strIcon = {"", "", ""}; private String[] strTemp = {"", "", ""}; private String[] strWind = {"", "", ""}; StringBuffer strResult = new StringBuffer(""); public BBWeatherForm(BBWeather bbweather) { super("BBWeather"); this.bbweather = bbweather; txtCity = new TextField("City", "北京", 20, 0); txtResult = new TextField("Result", "" , 100, 0); getCommand = new Command("View", Command.SCREEN, 1); append(txtCity); append(txtResult); addCommand(getCommand); setCommandListener(this); } public void commandAction(Command cmd, Displayable s) { if (cmd == getCommand) { getWeatherInfo(); } } private void getWeatherInfo() { //BBWeatherThread bbweatherthread = new BBWeatherThread(txtCity.getString()); //Thread thread = new Thread(bbweatherthread); //thread.start(); //while(thread.isAlive()); //if (!bbweatherthread.bOK) { // bbweather.alert(bbweatherthread.strErr); // return; //} //parseResult(bbweatherthread.strResult.toString()); String r = getInfo(); if (r != "") { bbweather.alert(r); return; } parseResult(strResult.toString()); if (strCity[0] == "") { txtResult.setString(strResult.toString().substring(8000)); bbweather.alert("无信息"); return; } //StringBuffer sb = new StringBuffer(""); //for (int i = 0; i < 3; i++) { //sb.append(strCity[i]); //sb.append("/n"); //sb.append(strDate[i]); //sb.append("/n"); //sb.append(strIcon[i]); //sb.append("/n"); //sb.append(strTemp[i]); //sb.append("/n"); //sb.append(strWind[i]); //sb.append("/n"); //} //txtResult.setString(sb.toString()); for (int i=0; i < 3; i++) { append(strCity[i]); append(strDate[i]); try { String[] icon = split(strIcon[i], "|"); for (int j=0; j < icon.length; j++) { if (icon[j] != "") { Image image; image = Image.createImage("/res/" + icon[j]); ImageItem ii = new ImageItem("", image, ImageItem.LAYOUT_CENTER, ""); append(ii); } } } catch(Exception e) { bbweather.alert(e.getMessage()); } append(strTemp[i]); append(strWind[i]); append("/n"); } } private void parseResult(String strResult) { String strTmp = strResult; if (strTmp == "") { txtResult.setString("无内容"); return; } if (strTmp.indexOf("天气信息不存在") != -1) { txtResult.setString("天气信息不存在"); return; } for (int i = 0; i < 3; i++) { //城市 if (strTmp.indexOf("City_Data") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("City_Data")); if (strTmp.indexOf("<h3>") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("<h3>") + 4); if (strTmp.indexOf("</h3>") == -1) return; strCity[i] = strTmp.substring(0, strTmp.indexOf("</h3>")); strTmp = strTmp.substring(strTmp.indexOf("</h3>")); //日期 if (strTmp.indexOf("<p>") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("<p>") + 3); if (strTmp.indexOf("</p>") == -1) return; strDate[i] = strTmp.substring(0, strTmp.indexOf("</p>")); strTmp = strTmp.substring(strTmp.indexOf("</p>")); //图标 if (strTmp.indexOf("Weather_Icon_B") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("Weather_Icon_B") + 16); while(strTmp.indexOf("figure/") != -1) { strTmp = strTmp.substring(strTmp.indexOf("figure/") + 7); strIcon[i] += strTmp.substring(0, strTmp.indexOf(".gif") + 4) + "|"; } //温度 if (strTmp.indexOf("Weather_TP") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("Weather_TP") + 12); if (strTmp.indexOf("</div>") == -1) return; strTemp[i] = strTmp.substring(0, strTmp.indexOf("</div>")); strTmp = strTmp.substring(strTmp.indexOf("</div>")); //风力 if (strTmp.indexOf("Weather_W") == -1) return; strTmp = strTmp.substring(strTmp.indexOf("Weather_W") + 11); if (strTmp.indexOf("</div>") == -1) return; strWind[i] = strTmp.substring(0, strTmp.indexOf("</div>")); strTmp = strTmp.substring(strTmp.indexOf("</div>")); } } private String getInfo() { String r; HttpConnection hc = null; InputStream is = null; try { String url = "http://php.weather.sina.com.cn/search.php?city=" + URLEncoder.encode(new String(txtCity.getString().getBytes("GB2312"), "ISO-8859-1"));// String url = "http://10.0.0.172:80/search.php?city=" + URLEncoder.encode(new String(txtCity.getString().getBytes("GB2312"), "ISO-8859-1")); hc = (HttpConnection)Connector.open(url); hc.setRequestMethod(HttpConnection.GET);// hc.setRequestProperty("X-Online-Host", "php.weather.sina.com.cn"); int status = hc.getResponseCode(); if (status == HttpsConnection.HTTP_OK) { is = hc.openInputStream(); int len = Integer.parseInt(hc.getHeaderField("Content-Length")); byte[] data = new byte[len]; int num = is.read(data); if (num > 0) strResult.append(new String(data, 0, num, "GB2312")); r = ""; } else { r = Integer.toString(status); } is.close(); hc.close(); }catch(Exception e) { r = e.getMessage(); } return r; } private String[] split(String s, String s1) { String as[] = null; int i = 1; int j = 0; int k = 0; int l = s.indexOf(s1); if(l != -1) { for(; l != -1; l = s.indexOf(s1, l + s1.length())) i++; as = new String[i]; for(int i1 = 0; i1 != -1;) { i1 = s.indexOf(s1, k); if(i1 != -1) { as[j++] = s.substring(k, i1); k = i1 + s1.length(); } else { as[j] = s.substring(k); } } } else { as = new String[1]; as[0] = s; } return as; }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: