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

老规矩,只有代码。   /* * 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;    }}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、5资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值