android天气预报实例

2014-11-16  本文已影响988人  研途更疯狂

学习Android有一个月了,最近打算用webserver写一个天气预报程序,主要是想熟悉一下网络通信编程这一块,选用的网站(点击查看)。


可以使用两种方式进行连接

  1. 导入ksoap2的jar包,连接webserver得到数据
  2. 使用Android自带的pull解析器得到数据

这里只提供连接的方法

代码块

private String CityName;
    private String URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
    private String NAMESPACE = "http://WebXml.com.cn/";
    private String METHOD_NAME = "getWeatherbyCityName";
    private String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
    //获取天气数据
    public SoapObject getSoapObject() throws HttpResponseException,
            IOException, XmlPullParserException {
        SoapObject result = null;
        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
        rpc.addProperty("theCityName", CityName);
        HttpTransportSE ht = new HttpTransportSE(URL);

        ht.debug = true;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = rpc;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(rpc);

        ht.call(SOAP_ACTION, envelope);
        result = (SoapObject) envelope.getResponse();
        return result;
    }
    //解析数据
    public String parserWeatherData(SoapObject rr) {

        if (rr != null) {
            int count = rr.getPropertyCount();
            //这里只是验证一下能否连接成功
            return rr.getProperty(count - 1).toString();
        }
        return null;
    }

代码块

//获取xml数据
public Weather getWeather() throws Exception {
        String Path = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=";
        URL url = new URL(Path + java.net.URLEncoder.encode(CityName, "UTF-8"));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == 200) {
            InputStream instream = conn.getInputStream();
            return parserXML(instream);
        }
        return null;
    }
//解析xml数据
    private Weather parserXML(InputStream instream) throws Exception {
        String[] weatherData = new String[23];
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(instream, "UTF-8");
        int event = parser.getEventType();
        int i = 0;
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_TAG:
                if ("string".equals(parser.getName())) {
                    weatherData[i] = parser.nextText();
                    i++;
                }
                break;
            case XmlPullParser.END_TAG:
                if ("string".equals(parser.getName())) {

                }
                break;
            }
            event = parser.next();
        }

        Weather weatherBean = new Weather();
        weatherBean.City = weatherData[1];
        weatherBean.TempratureRange = weatherData[5];
        weatherBean.Date = weatherData[4];
        String[] ss = weatherData[10].split(";");
        String[] ssT = ss[0].split(":");
        weatherBean.Temperature = ssT[2];
        int length = ss.length;
        for (int j = 0; j < length; j++) {
            Log.d("weatherService", ss[j]);
        }
        Log.d("weatherService", "length=" + length);
        if (length == 5) {
            weatherBean.Wind = ss[1];
            weatherBean.Dampness = ss[2];
            weatherBean.Rays = ss[4];
        }
        String[] ssGs = weatherData[6].split(" ");
        weatherBean.GaiKuang = ssGs[1];

        weatherBean.GaiKuang2 = weatherData[13].replace(" ", "\n");
        weatherBean.Temprature2 = weatherData[12];
        weatherBean.Wind2 = weatherData[14];

        weatherBean.GaiKuang3 = weatherData[18].replace(" ", "\n");
        weatherBean.Temprature3 = weatherData[17];
        weatherBean.Wind3 = weatherData[19];

        String[] ti2 = weatherData[16].split("\\.");
        weatherBean.Image2 = ti2[0];
        String[] ti3 = weatherData[21].split("\\.");
        weatherBean.Image3 = ti3[0];

        return weatherBean;

    }

具体的weather类和界面代码可以自行下载,点击这里

上一篇下一篇

猜你喜欢

热点阅读