Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

通過 WebService 獲取天氣預報數據的實例分享

編輯:關於 Android 程式設計

WebService 是 Android 與服務端交互的重要方式之一。本文以查詢天氣為例,演示如何透過 ksoap2 調用遠程 WebService 接口,解析返回的複雜對象並展示天氣信息與對應圖標。

WeatherMain.java 核心代碼:

package tao.cs;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WeatherMain extends Activity {
    Button btn01;
    TextView tv01, tv02;
    ImageView imgview01, imgview02;
    EditText et01;
    int int_img_1;
    int int_img_2;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn01 = (Button) findViewById(R.id.Button01);
        tv01 = (TextView) findViewById(R.id.TextView01);
        tv02 = (TextView) findViewById(R.id.TextView02);
        imgview01 = (ImageView) findViewById(R.id.ImageView01);
        imgview02 = (ImageView) findViewById(R.id.ImageView02);
        et01 = (EditText) findViewById(R.id.EditText01);
        btn01.setOnClickListener(new OnClickListener() {
             public void onClick(View v) { showWeather(); }
         });
   }
    protected void showWeather(){
        String str_city = et01.getText().toString();
        if (str_city.length() == 0) { str_city = "重慶"; }
        getWeather(str_city);
        tv01.setText(getString_WeatherToday());  //今天天氣
        tv02.setText(getString_WeatherCurrent());//當前天氣
        imgview01.setImageResource(getIcon_1());  //當前的兩個圖標
        imgview02.setImageResource(getIcon_2());
   }
    private static final String NAME_SPACE = "https://WebXml.com.cn/";
    private static final String METHOD_NAME = "getWeatherbyCityName";
    private static String URL = "https://www.webxml.com.cn/webservices/weatherwebservice.asmx";
    private static String SOAP_ACTION = "https://WebXml.com.cn/getWeatherbyCityName";
    protected void getWeather(String strcityname){
        SoapObject sobject = new SoapObject(NAME_SPACE, METHOD_NAME);
        sobject.addProperty("theCityName", strcityname);
        AndroidHttpTransport ht = new AndroidHttpTransport(URL);
        ht.debug = true;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = sobject;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(sobject);
        try {
            ht.call(SOAP_ACTION, envelope);
        } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); }
        SoapObject result = (SoapObject) envelope.bodyIn;
        SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
        parseWeather(detail);
   }
    String str_weather_today = null;
    String str_weather_current = null;
    protected void parseWeather(SoapObject so){
        // 第六個參數是概況,包括日期、天氣等
        String date = so.getProperty(6).toString();
        str_weather_today = "今天: " + date.split(" ")[0];
        str_weather_today = str_weather_today + "\n氣溫:" + so.getProperty(5).toString();
        str_weather_today = str_weather_today + "\n風力:" + so.getProperty(7).toString() + "\n";
        int_img_1 = parseIcon(so.getProperty(8).toString());
        int_img_2 = parseIcon(so.getProperty(9).toString());
        str_weather_current = so.getProperty(10).toString();
   }
    public String getString_WeatherToday(){ return str_weather_today; }
    public String getString_WeatherCurrent(){ return str_weather_current; }
    public int getIcon_1(){ return int_img_1; }
    public int getIcon_2(){ return int_img_2; }
    public int parseIcon(String strIcon){
        if (strIcon == null) return -1;
        if ("0.gif".equals(strIcon)) return R.drawable.a_0;
        if ("1.gif".equals(strIcon)) return R.drawable.a_1;
        /* … 依此類推,直到 a_31 … */
        return 0;
   }
}

布局文件 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
<Button android:text="查詢" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<ImageView android:id="@+id/ImageView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView01" />
<TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

整體流程是:輸入城市名 → 組裝 SoapObject 請求 → 透過 AndroidHttpTransport 調用遠程方法 → 解析返回的複雜節點 → 更新界面上的天氣文字與圖標。其中 dotNet=true 是因為該服務基於 .NET 實現,需要特別設置。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved