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

我的第一個 Android 存取 Java WebService 程式(ksoap2)

編輯:關於 Android 程式設計

本文記錄如何用 Android 呼叫一個以 Java 撰寫的 WebService,工具使用 MyEclipse,Android 端則採用 ksoap2 函式庫。

一、用 Java 打造 WebService

工具:MyEclipse。步驟如下:

  1. 從工具列 File → New → Web Service Project 建立專案。
  2. 採用 JAX-WS 方式。
  3. 撰寫程式碼後發布 WS(new web service,MyEclipse 工具列圖示),記得勾選自動產生 wsdl 檔。
  4. 匯入 MyEclipse 自帶的兩個 JAX-WS jar 檔。
  5. 部署專案,完成。

總結來說,程式碼撰寫方式和寫一般 Java 一樣,其餘的 wsdl、部署等雜事都由工具代勞,網上文件很多,照著做即可。

二、Android 端程式碼

Android 端透過 ksoap2 組出 SOAP 請求、呼叫遠端方法並解析回傳值。以下以呼叫 getPwd 方法為例:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
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.TextView;

public class MainActivity extends Activity {
    private TextView textView;
    private EditText editText;
    private Button button;
    private static final String serviceURL = "https://172.20.0.129:7070/UserMgService/UserMgServicePort";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        button.setText("獲取服務器端數據");
        button.setOnClickListener(new MyButtonListener());
    }

    class MyButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            editText.setText(getRemoteInfo());
            textView.setText("服務器端數據:" + getRemoteInfo());
        }
    }

    public String getRemoteInfo() {
        String out = "11";  // 命名空間
        try {
            String nameSpace = "https://service.jcy/";  // 調用的方法名稱
            String methodName = "getPwd";
            String endPoint = "https://172.20.0.129:7070/UserMgService/UserMgServicePort";
            // String soapAction = "https://service.jcy/getUserInfo";
            // 指定 WebService 的命名空間和調用的方法名
            SoapObject rpc = new SoapObject(nameSpace, methodName);
            // 設置需調用 WebService 接口需要傳入的參數
            // rpc.addProperty("mobileCode", phoneSec);
            // rpc.addProperty("userId", "");
            // 生成調用 WebService 方法的 SOAP 請求信息,並指定 SOAP 的版本
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            // 設置是否調用的是 dotNet 開發的 WebService
            // envelope.dotNet = true;
            // 等價於 envelope.bodyOut = rpc;
            (new MarshalBase64()).register(envelope);
            AndroidHttpTransport transport = new AndroidHttpTransport(serviceURL);
            transport.debug = true;
            transport.call(nameSpace + methodName, envelope);
            if (envelope.getResponse() != null) {
                out = envelope.getResponse().toString();
            }
        } catch (SoapFault e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return out;
    }
}

整體流程是:先用 SoapObject 指定命名空間與方法名,需要傳參數時用 addProperty 加入;接著建立 SoapSerializationEnvelope 並指定 SOAP 版本(若對方是 .NET 服務則要設 envelope.dotNet = true);最後用 AndroidHttpTransport 送出請求,從 envelope.getResponse() 取得回傳結果。記得在 Manifest 加上網路權限,並注意 this 這種耗時網路操作應放在非主執行緒執行,以免造成 ANR。

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