Android利用ksoap2写天气预报应用

Android利用ksoap2写天气预报应用
2010-06-01 18:06

  怎么说呢,天气预报的应用可以说是一抓一大把,不过找一个自己心仪的却并不是很容易。这个程序也是用来练手的,看看到底怎么用ksoap写一个适合自己的应用。ksoap怎么说呢,就是一个封装好的soap发送接收解析库,这也只怪Android没有自带这些。

  闲话不多说,上代码(本来是全部贴出来的,后来发现博客毕竟是用来写重点的,更多内容还是访问下面那个给出的源码地址吧):

    
private static final String NAMESPACE = "http://WebXml.com.cn/";    
    private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    private static final String METHOD_NAME = "getWeatherbyCityName";
    private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";

    public void getWeather(String cityName) {
        try {
            SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
            rpc.addProperty("theCityName", cityName);

            AndroidHttpTransport ht = new AndroidHttpTransport(URL);
            ht.debug = true;

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

            ht.call(SOAP_ACTION, envelope);
            
            debug(LOG_TAG, "DUMP>> " + ht.requestDump);
            debug(LOG_TAG, "DUMP<< " + ht.responseDump);

            SoapObject result = (SoapObject) envelope.bodyIn;
            SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
            
            parseWeather(detail);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void parseWeather(SoapObject detail) {
        String date = detail.getProperty(6).toString();
        weatherToday = "今天:" + date.split(" ")[0];
        weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
        weatherToday = weatherToday + "\n气温:" + detail.getProperty(5).toString();
        weatherToday = weatherToday + "\n风力:" + detail.getProperty(7).toString() + "\n";
        iconToday[0] = parseIcon(detail.getProperty(8).toString());
        iconToday[1] = parseIcon(detail.getProperty(9).toString());
        
        weatherCurrent = detail.getProperty(10).toString();
        
        date = detail.getProperty(13).toString();
        weatherTomorrow = "明天:" + date.split(" ")[0];
        weatherTomorrow = weatherTomorrow + "\n天气:" + date.split(" ")[1];
        weatherTomorrow = weatherTomorrow + "\n气温:" + detail.getProperty(12).toString();
        weatherTomorrow = weatherTomorrow + "\n风力:" + detail.getProperty(14).toString() + "\n";
        iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());
        iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());
        
        date = detail.getProperty(18).toString();
        weatherAfterday = "后天:" + date.split(" ")[0];
        weatherAfterday = weatherAfterday + "\n天气:" + date.split(" ")[1];
        weatherAfterday = weatherAfterday + "\n气温:" + detail.getProperty(17).toString();
        weatherAfterday = weatherAfterday + "\n风力:" + detail.getProperty(19).toString() + "\n";
        iconAfterday[0] = parseIcon(detail.getProperty(20).toString());
        iconAfterday[1] = parseIcon(detail.getProperty(21).toString());
    }
以下是截图:
Android利用ksoap2写天气预报应用

  觉得还不够?是的,毕竟博客能写多少呢?给两个地址。当然代码写得非常不够简洁,还请海涵啊。

apk:http://u.115.com/file/f020b51648

源码:http://u.115.com/file/f0c9f83996

原创文章,如需转载请注明【转烛空间】:http://wangliping.net

你可能感兴趣的:(android,.net,SOAP)