編輯:關於 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 實現,需要特別設置。
Android 調用系統郵件元件(Intent 匹配流程解析)
本文說明如何透過 Intent 調用系統郵件元件,並釐清 Intent 的匹配流程。 程式碼如下: public class MainActivity extends A
Android 增量更新實戰:bsdiff + CMake 構建 so
一直想搞明白增量更新是怎麼回事,趁著查資料的機會折騰了幾天,順手把 NDK 開發也練了一遍。 效果預覽 環境 Android Studio 2.2.1 for Wi
透解 Android View 繪製流程與自訂 ViewGroup:三種動態移位方案的取捨
本文從 ViewGroup/View 的繪製流程談起,說明自訂 ViewGroup 的兩種常見寫法,最後整理出三種動態改變 View 位置的實作方式,並比較其優劣。 一
Behavior 實戰:用 CoordinatorLayout 做出知乎風格的上滑隱藏動畫
這篇要仿的是知乎首頁:向上滑動時 Toolbar、FloatingActionButton 與底部 Tab 導航一起隱藏,向下滑動時再回來。如果你的期望不是這個,那可以直