Android個人學習筆記-使用myeclipse快速建立webservice並在Android中調用

來源:互聯網
上載者:User

Android個人學習筆記-使用myeclipse快速建立webservice並在Android中調用

Web service是一個平台獨立的,低耦合的,自包含的、基於可程式化的web的應用程式,可使用開放的XML(標準通用標記語言 (SGML)下的一個子集)標準來描述、發布、發現、協調和配置這些應用程式,用於開發分布式的互操作的應用程式。(來源百度百科)

很早之前就使用過myeclipse建立JAX-WS規範的webservice(Java API for XML Web Services (JAX-WS)是Java程式設計語言一個用來建立Web服務的API。點擊開啟連結),在百度中也有很多現成的例子可供參考,作為初學者,一步一步的按照教程做並且分步的去瞭解所涉及的知識點是很重要的,下面就詳細的介紹整個過程。

1、webservice的建立

在建立之前先看一下工程的目錄結構:

主要注意JAX-WS 2.1 的兩個選項,在建立完成後應該去手動添加這兩個選項,接下來會解釋。

本項目主要是與Android通訊,主要有一個介面,一個getInfoList(double x,double y)方法,其中x和y是經緯度,通過Android端定位獲得的經緯度,然後與資料庫端的資訊進行計算,即得到不同的地點離我的距離,然後返回用戶端一個json字串,字串的格式類型見net.zmqc.bean Information類中。

本項目主要是與資料庫的串連,所以需要對不同的類分包存放,但是對外介面使用net.zmqc.service裡的類,下面把所有的類進行粘貼,再這之前先通過一個簡單的測試類別來說明建立一個webservice的過程,這步就是需要你寫你要使用的類,例如你可以簡單的寫一個Test類,然後在Test類中定義一個方法或多個方法

 

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,點擊可以在最下方找到文章開頭說的內容

到此簡單的測試類別的服務的發布就完成了,接下來看一下開頭工程中供Android使用的項目中各個類

com.zmqc.dao:

 

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 getInfoList(double x,double y){List infolist = new ArrayList();Information info = new Information();try {ct = new DbConn().getConn();     Stringsql=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));String ss = info.toString();infolist.add(ss);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{new CloseDbConn().close(ct,rs,ps);}return infolist;}}

com.zmqc.tool

 

 

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);try {ct=DriverManager.getConnection(jdbc:mysql://localhost:3306/infogps?user=root&password=123456);System.out.println(link success!!!);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (ClassNotFoundException e) {e.printStackTrace();}return ct;}public void close(){try {if(rs!=null)rs.close();rs=null;ps.close();ps=null;ct.close();ct=null;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}

package net.zmqc.tool;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;public class CloseDbConn {public Connection ct;public  void close(Connection ct,ResultSet rs,PreparedStatement ps){ try {if(rs!=null)rs.close();rs=null;ps.close();ps=null;ct.close();ct=null;System.out.println(link break!!!);} catch (Exception e) {e.printStackTrace();}}}
該類是通過經緯度計算兩點距離,主要是針對具體的需要產生的
package net.zmqc.tool;public class DistanceCom {public static double computeDistance(double lat1, double lon1,            double lat2, double lon2) {            // Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf            // using the Inverse Formula (section 4)            int MAXITERS = 20;            // Convert lat/long to radians            lat1 *= Math.PI / 180.0;            lat2 *= Math.PI / 180.0;            lon1 *= Math.PI / 180.0;            lon2 *= Math.PI / 180.0;            double a = 6378137.0; // WGS84 major axis            double b = 6356752.3142; // WGS84 semi-major axis            double f = (a - b) / a;            double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);            double L = lon2 - lon1;            double A = 0.0;            double U1 = Math.atan((1.0 - f) * Math.tan(lat1));            double U2 = Math.atan((1.0 - f) * Math.tan(lat2));            double cosU1 = Math.cos(U1);            double cosU2 = Math.cos(U2);            double sinU1 = Math.sin(U1);            double sinU2 = Math.sin(U2);            double cosU1cosU2 = cosU1 * cosU2;            double sinU1sinU2 = sinU1 * sinU2;            double sigma = 0.0;            double deltaSigma = 0.0;            double cosSqAlpha = 0.0;            double cos2SM = 0.0;            double cosSigma = 0.0;            double sinSigma = 0.0;            double cosLambda = 0.0;            double sinLambda = 0.0;            double lambda = L; // initial guess            for (int iter = 0; iter < MAXITERS; iter++) {                double lambdaOrig = lambda;                cosLambda = Math.cos(lambda);                sinLambda = Math.sin(lambda);                double t1 = cosU2 * sinLambda;                double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;                double sinSqSigma = t1 * t1 + t2 * t2; // (14)                sinSigma = Math.sqrt(sinSqSigma);                cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)                sigma = Math.atan2(sinSigma, cosSigma); // (16)                double sinAlpha = (sinSigma == 0) ? 0.0 :                    cosU1cosU2 * sinLambda / sinSigma; // (17)                cosSqAlpha = 1.0 - sinAlpha * sinAlpha;                cos2SM = (cosSqAlpha == 0) ? 0.0 :                    cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)                double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn                A = 1 + (uSquared / 16384.0) * // (3)                    (4096.0 + uSquared *                     (-768 + uSquared * (320.0 - 175.0 * uSquared)));                double B = (uSquared / 1024.0) * // (4)                    (256.0 + uSquared *                     (-128.0 + uSquared * (74.0 - 47.0 * uSquared)));                double C = (f / 16.0) *                    cosSqAlpha *                    (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)                double cos2SMSq = cos2SM * cos2SM;                deltaSigma = B * sinSigma * // (6)                    (cos2SM + (B / 4.0) *                     (cosSigma * (-1.0 + 2.0 * cos2SMSq) -                      (B / 6.0) * cos2SM *                      (-3.0 + 4.0 * sinSigma * sinSigma) *                      (-3.0 + 4.0 * cos2SMSq)));                lambda = L +                    (1.0 - C) * f * sinAlpha *                    (sigma + C * sinSigma *                     (cos2SM + C * cosSigma *                      (-1.0 + 2.0 * cos2SM * cos2SM))); // (11)                double delta = (lambda - lambdaOrig) / lambda;                if (Math.abs(delta) < 1.0e-12) {                    break;                }            }            return  b * A * (sigma - deltaSigma);}}
net.zmqc.bean
package net.zqmc.bean;public class Information {private int g_id;private String g_date;private String g_place;private String g_intro;private String g_imgsrc;private String g_detail;private double g_distance;public int getG_id() {return g_id;}public void setG_id(int g_id) {this.g_id = g_id;}public String getG_date() {return g_date;}public void setG_date(String g_date) {this.g_date = g_date;}public String getG_place() {return g_place;}public void setG_place(String g_place) {this.g_place = g_place;}public String getG_intro() {return g_intro;}public void setG_intro(String g_intro) {this.g_intro = g_intro;}public String getG_imgsrc() {return g_imgsrc;}public void setG_imgsrc(String g_imgsrc) {this.g_imgsrc = g_imgsrc;}public String getG_detail() {return g_detail;}public void setG_detail(String g_detail) {this.g_detail = g_detail;}public double getG_distance() {return g_distance;}public void setG_distance(double g_distance) {this.g_distance = g_distance;} @Override      public String toString() {          return {g_id= + g_id + , g_date= + g_date + , g_intro= + g_intro                  + , g_imgsrc= + g_imgsrc +, g_detail= + g_detail + , g_distance= + g_distance +, g_place= + g_place +};      }  }

package net.zqmc.bean;public class Place {private int p_id;private double p_x;private double p_y;private String p_name;public int getP_id() {return p_id;}public void setP_id(int p_id) {this.p_id = p_id;}public double getP_x() {return p_x;}public void setP_x(double p_x) {this.p_x = p_x;}public double getP_y() {return p_y;}public void setP_y(double p_y) {this.p_y = p_y;}public String getP_name() {return p_name;}public void setP_name(String p_name) {this.p_name = p_name;}}
net.zmqc.service 該包裡面的類也即是如Test類一樣,對它進行發布

package net.zmqc.service;import java.util.List;import net.zmqc.dao.InfoDao;public class InfoService {public List getInfoList(double x,double y){return InfoDao.getInfoList(x,y);}}

然後可以通過快速建立用戶端來測試服務是否成功,並可調試

 

在項目中點擊右鍵,點擊other,找到web service client 點擊,出現

可以選擇通過目錄中的例如H:WorkspacesMyEclipse 10InfoGpsServiceWebRootWEB-INFwsdlInfoServiceService.wsdl

或者直接通過http://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));}}
可以進行簡單的測試。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.