zoukankan      html  css  js  c++  java
  • Android天气预报

    Android天气预报

    1、指定 WebService 的命名空间和调用方法
    import org.ksoap2.serialization.SoapObject;
    private static final String NAMESPACE = "
    http://WebXml.com.cn/";
    private static final String METHOD_NAME = "getWeatherbyCityName";
    SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。
    第二个参数表示要调用的WebService方法名。
    2、设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:
    rpc.addProperty("theCityName", "北京");
    要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
    3、生成调用Webservice方法的SOAP请求信息。
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = rpc;envelope.dotNet = true;envelope.setOutputSoapObject(rpc);
    创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
    该版本号需要根据服务端WebService的版本号设置。
    在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,
    该属性的值就是在第一步创建的SoapObject对象。
    4、创建HttpTransportsSE对象。
    这里不要使用 AndroidHttpTransport ht = new AndroidHttpTransport(URL); 这是一个要过期的类
    private static String URL = "
    http://www.webxml.com.cn/webservices/weatherwebservice.asmx";HttpTransportSE ht = new HttpTransportSE(URL); ht.debug = true;
    5、使用call方法调用WebService方法
    private static String SOAP_ACTION = "
    http://WebXml.com.cn/getWeatherbyCityName";ht.call(SOAP_ACTION, envelope);
    网上有人说这里的call的第一个参数为null,但是经过我的测试,null是不行的。
    第2个参数就是在第3步创建的SoapSerializationEnvelope对象。
    6、获得WebService方法的返回结果
    有两种方法:
    (1)、使用getResponse方法获得返回数据。
    private SoapObject detail;
    detail =(SoapObject) envelope.getResponse();
    (2)、使用 bodyIn 及 getProperty。
    private SoapObject detail;
    SoapObject result = (SoapObject)envelope.bodyIn;detail = (SoapObject) result.getPropert ("getWeatherbyCityNameResult");
    7、 这时候执行会出错,提示没有权限访问网络
    需要修改 AndroidManifest.xml 文件,赋予相应权限
    简单来说就是增加下面这行配置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    完整的 AndroidManifest.xml 文件 如下:
    注:Android 中在代码中为了调试写了system.out.print()输出项
    在菜单:Window-->show view-->other-->找到Android,选择Logcat 是可以看到输出的,
    如果你想在一个单独的窗口看到system.out.print()的输出的话,可以在logcat界面点那个绿色的“+”好,
    在Filter name 和 By log tag里面均填入System.out,这样的话你就能在单独的界面查看system.out.print()的输出了!!

    主要代码:

    date.setText(weatherarr[1]);
    today.setText(weatherarr[5]+"
    "+weatherarr[6]+"
    "+weatherarr[7]);  //今天天气
    tomorrow.setText(weatherarr[12]+"
    "+weatherarr[13]+"
    "+weatherarr[14]);  //明天天气
    after.setText(weatherarr[17]+"
    "+weatherarr[18]+"
    "+weatherarr[19]);  //后天天气
    //Log.i("cc",weatherarr[8]);
    todayimage.setImageResource(WeatherUtil.getImage(weatherarr[8]));
    tomrrowimage.setImageResource(WeatherUtil.getImage(weatherarr[15]));
    afterimage.setImageResource(WeatherUtil.getImage(weatherarr[20]));
    
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("theCityName", cityNo);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.bodyOut = request;
    envelope.dotNet = true; 
    
    HttpTransportSE transport = new HttpTransportSE(URL);
    transport.debug = true;
    transport.call(NAMESPACE + METHOD_NAME, envelope); 
    
    SoapObject result = (SoapObject) envelope.getResponse();
    int count = result.getPropertyCount();
    arr=new String[count];
    for (int i = 0; i < count; i++) {
        arr[i]=result.getProperty(i).toString();
    }

    参考链接:http://www.apkbus.com/forum.php?mod=viewthread&tid=52971

    代码下载链接:http://download.csdn.net/detail/klcf0220/6458907(推荐,示例代码。)

    http://www.apkbus.com/forum.php?mod=viewthread&tid=143260

    http://www.apkbus.com/forum.php?mod=viewthread&tid=143186

  • 相关阅读:
    HDU 1081 To The Max (DP) 扩展最大子列和,求最大子矩阵和
    How to Log SOAP Message Content in Metro?(官方解答)
    纪晓岚与和珅对调后的工作困难
    蒙版
    2011年选拔赛C本科
    网络常用语
    linux怎么用一个命令行统计出给定目录中有多少个子目录
    REGSVR32
    香水
    判断一个数是否是2的平方
  • 原文地址:https://www.cnblogs.com/klcf0220/p/3388961.html
Copyright © 2011-2022 走看看