• Android网络编程之Web Service获取天气预报( 获取省市填充下拉列表)


    本程序利用 http://WebXml.com.cn/ 提供的天气预报服务来编写一个Web Service应用的小例子

    编写过程主要分两步:1.用获得的省市信息填充一个下拉列表; 2. 点击一个城市,在列表下方的ScrollView中显示出具体的天气等信息

    首先列出布局文件:

    View Code
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        
        <Spinner 
            android:id="@+id/citySpinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
        
        <ScrollView 
            android:id="@+id/weatherInfoView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            
            <TextView 
                android:id="@+id/weatherInfoText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />        
            
        </ScrollView>
        
    </LinearLayout>

    然后是主Activity组件准备:

    View Code
    public class MainActivity extends Activity {
        
        private Spinner citySpinner = null;
        private TextView weatherInfoText = null;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            /*
             * 下面两段代码使Android3.0以上系统可以让网络操作代码使用主UI线程,
             * 因为3.0以上系统对UI资源的使用更严格,不允许耗时操作在UI线程进行
             */
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
            .detectDiskReads().detectDiskWrites().detectNetwork() 
            .penaltyLog().build()); 
    
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects().penaltyLog()
            .penaltyDeath().build()); 
    
            citySpinner = (Spinner) findViewById(R.id.citySpinner);
            weatherInfoText = (TextView) findViewById(R.id.weatherInfoText);
            
            // 调用自己写的辅助类中的getCityList()获取中国省市名称列表
            List<String> cityList = WebServiceUtil.getCityList();
            // ArrayAdapter填充下拉列表
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, cityList);
            citySpinner.setAdapter(adapter);
            
            citySpinner.setOnItemSelectedListener(new CityChoosedListener());
        }

    这里我们先不看下拉列表的绑定事件,而是先处理填充它

    下面是WebServiceUtil工具类,我们把获取Web service的返回结果,解析返回结果都放在此类中运行。

    先是获取和解析省市信息:

    View Code
    public class WebServiceUtil {
        // 命名空间
        private static final String NAMESPACE = "http://WebXml.com.cn/";
        // 请求服务的地址
        private static final String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
        // 请求服务的action
        private static final String SOAP_ACTION  = "http://WebXml.com.cn/getRegionProvince";
        // 调用的方法名
        private static final String METHOD_NAME = "getRegionProvince";
        
        
        /*获取城市省份列表*/
        public static List<String> getCityList() {
            
            List<String> list = null;
            // 获得SoapObject对象
            SoapObject so = new SoapObject(NAMESPACE, METHOD_NAME);
            // 获得Enveloper对象
            SoapSerializationEnvelope sse = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            sse.bodyOut = so;
            sse.dotNet = false;
            sse.setOutputSoapObject(so);
            
            // AndroidHttpTransport类已经过期不再使用
            HttpTransportSE htse = new HttpTransportSE(URL);
            try {
                htse.call(SOAP_ACTION, sse);
                // 判断有无回应,若有,调用方法解析返回的xml
                if (sse.getResponse() != null) {
                    // 返回数据均为XML格式,要根据具体格式从中提取出所需字符串
                    list = parseList(sse.bodyIn.toString());
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            return list;
        }
        
        /*
         * 解析传回的城市省份xml文件
         * 这里采用的方法是把整个xml文件中的所有省份文字部分单独提取出来,放入List中
         */
        private static List<String> parseList(String str) {
            String temp = "";
            List<String> list = new ArrayList<String>();
            if (str != null && str.length() > 0) {
                int start = str.indexOf("string");
                int end = str.lastIndexOf(";");
                temp = str.substring(start, end - 3);
                String[] test = temp.split(";");
                for (int i = 0; i < test.length; i++) {
                    if (i == 0) {
                        temp = test[i].substring(7);
                    } else {
                        temp = test[i].substring(8);
                    }
                    int index = temp.indexOf(",");
                    list.add(temp.substring(0, index));
                }
            }
            return list;
        }

    执行后效果如图:

  • 相关阅读:
    centos7时间不同步,ntpd时间同步服务
    centos7.6离线安装docker
    安装虚拟机总是获取不到IP地址的解决办法
    安装英伟显卡,出现报错总结
    在ubuntu 安装make
    if,while,for脚本小练习
    python输入密码-隐藏
    Python修改headers参数的两种方法
    Python爬虫 —POST请求有道翻译{"errorcode":50}
    列表转换为字典
  • 原文地址:https://www.cnblogs.com/moka/p/3069430.html
Copyright © 2020-2023  润新知