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

Android 學習筆記——用 MyEclipse 快速創建 WebService(上)

編輯:關於 Android 程式設計

Web Service 是一個平台獨立、低耦合、自包含、基於可程式化 Web 的應用程式,可使用開放的 XML 標準來描述、發布、發現、協調與配置,用於開發分散式、可互操作的應用程式。

我很早就用 MyEclipse 創建過基於 JAX-WS 規範的 WebService(JAX-WS 是 Java 中用來創建 Web 服務的一組 API)。網路上也有大量現成範例可供參考。對初學者而言,一步一步照著教程做、並分階段理解涉及的知識點非常重要。下面詳細介紹整個過程。

一、WebService 的創建

創建之前,先看一下工程的目錄結構:

要特別注意 JAX-WS 2.1 的兩個選項,創建完成後應手動補上這兩個選項,後文會解釋。

本專案主要用於與 Android 通信,對外提供一個 getInfoList(double x, double y) 方法,其中 x、y 是經緯度。Android 端定位取得經緯度後,與資料庫中的資訊進行計算,得出各地點離「我」的距離,再回傳一個 JSON 字串給客戶端(格式見 Information 類別)。

由於要連接資料庫,需要對不同類別分包存放,而對外接口則放在 net.zmqc.service 中。先用一個簡單的測試類說明創建 WebService 的過程。你需要先寫好要發布的類別,例如:

public class Test {
    public static String getReply(String ss) {
        return ss;
    }
}

然後在專案上單擊右鍵:

單擊 Other 才會出現上圖;接著單擊 Web Service,選擇 from class 並單擊下一步:

找到你要發布的類別,勾選 Generate WSDL in project,然後單擊 OK 完成:

此時你可以在工程中看到新增的內容:

接著在 Build Path 中選擇 Configure Build Path:

點擊 Add Library,找到 MyEclipse Librarys,即可在最下方找到開頭提到的 JAX-WS 選項:

至此,一個簡單測試類的服務發布就完成了。接下來看供 Android 使用的各個類別。

資料訪問層 InfoDao:

package net.zmqc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import net.zmqc.tool.CloseDbConn;
import net.zmqc.tool.DbConn;
import net.zmqc.tool.DistanceCom;
import net.zqmc.bean.Information;

public class InfoDao {
    private static Connection ct = null;
    private static ResultSet rs = null;
    private static PreparedStatement ps = null;

    public static List<String> getInfoList(double x, double y) {
        List<String> infolist = new ArrayList<String>();
        Information info = new Information();
        try {
            ct = new DbConn().getConn();
            String sql = "select * from information,place where information.g_place = place.p_id";
            ps = ct.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                info.setG_id(rs.getInt("g_id"));
                info.setG_date(rs.getString("g_date"));
                info.setG_detail(rs.getString("g_detail"));
                double dis = DistanceCom.computeDistance(x, y, rs.getDouble("p_x"), rs.getDouble("p_y"));
                info.setG_distance(dis);
                info.setG_imgsrc(rs.getString("g_imgsrc"));
                info.setG_intro(rs.getString("g_intro"));
                info.setG_place(rs.getString("p_name"));
                infolist.add(info.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            new CloseDbConn().close(ct, rs, ps);
        }
        return infolist;
    }
}

工具類包括資料庫連接 DbConn、關閉連接 CloseDbConn,以及依經緯度計算兩點距離的 DistanceCom(基於 WGS84 的 Vincenty 公式實現)。由於篇幅關係,這裡重點呈現結構:

package net.zmqc.tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbConn {
    private Connection ct;
    public ResultSet rs = null;
    public PreparedStatement ps = null;

    public Connection getConn() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            ct = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/infogps", "root", "123456");
            System.out.println("link success!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ct;
    }
}

資料模型 Information 封裝了地點的編號、日期、名稱、介紹、圖片、詳情與距離等欄位,並複寫 toString() 以便輸出為字串;Place 則封裝地點的座標與名稱。

對外服務類 InfoService(與 Test 類別一樣,對它進行發布):

package net.zmqc.service;

import java.util.List;
import net.zmqc.dao.InfoDao;

public class InfoService {
    public List<String> getInfoList(double x, double y) {
        return InfoDao.getInfoList(x, y);
    }
}

之後可以透過快速建立客戶端來測試服務是否成功:

在專案上單擊右鍵,選擇 Other,找到 Web Service Client:

可以選擇本地 WSDL 檔,或直接用 https://localhost:8080/InfoGpsService/InfoServicePort?wsdl 取得,最後單擊完成即可。然後透過測試類就能調用:

package net.zmqc.test;

import net.zmqc.service.InfoServiceDelegate;
import net.zmqc.service.InfoServiceService;

public class test {
    public static void main(String[] args) {
        InfoServiceService ef = new InfoServiceService();
        InfoServiceDelegate eif = ef.getInfoServicePort();
        System.out.println(eif.getInfoList(49.327458, 121.349691));
    }
}

這樣就能進行簡單的測試了。

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