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

Android 透過 WebService(SOAP)連接 SQL Server 資料庫

編輯:關於 Android 程式設計

以前做專案只要用到資料庫,都在 App 裡自建一份資料,但這麼做並不可取:手機記憶體有限,把資料表建在專案裡無疑又增大了程式體積,運行速度可想而知;而且資料量大時放在資料庫更合適,不僅方便還能達到同步效果。

很多應用所依賴的資料都存在資料庫裡,這時既精簡又能同步到資料庫的方法,就是透過 WebService(SOAP 協議)連線取得資料庫資訊。以下做個小範例。

版面配置

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText android:id="@+id/name"
            android:layout_width="200dp"
            android:layout_height="wrap_content"/>
        <Button android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <TextView android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

SOAP 工具類

public class SOAPUtil {
    public static Object doTransport(final String wsdUrl, final String webMethod) {
        String nameSpace = "https://tempuri.org/"; // 一般都是預設的
        SoapObject soapObject = new SoapObject(nameSpace, webMethod);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyIn = soapObject;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTransportSE = new HttpTransportSE(wsdUrl);
        String SOAP_ACTION = "https://tempuri.org/" + webMethod;
        try {
            httpTransportSE.call(SOAP_ACTION, envelope);
            if (envelope.getResponse() != null) {
                SoapObject result = (SoapObject) envelope.getResponse();
                for (int i = 0; i < result.getPropertyCount(); i++) {
                    System.out.println("result [" + i + "] = " + result.getProperty(i).toString());
                }
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }
}

主要实现

public class MainActivity extends Activity {
    private Button searchs;
    private TextView results;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        searchs = (Button) findViewById(R.id.search);
        results = (TextView) findViewById(R.id.result);
        searchs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String wsdUrl = "https://192.168.1.195:88/service1.asmx"; // 伺服器位址
                String method = "SelectAll";                            // 方法名
                Object result = SOAPUtil.doTransport(wsdUrl, method);
                results.setText(result.toString());
            }
        });
    }
}

以上是客戶端必要步驟,此外還需伺服器端提供 WebService 介面(介面名即 Activity 中呼叫的方法名)。介面本身並不複雜,各語言都能實作,核心就是 SQL 操作語句,寫好部署到伺服器即可。此範例使用 ksoap2 相關類別,運作前需在專案中加入對應的第三方函式庫,並記得加入網路權限。

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