标签:
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。(来源百度百科)
很早之前就使用过myeclipse创建JAX-WS规范的webservice(Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API。点击打开链接),在百度中也有很多现成的例子可供参考,作为初学者,一步一步的按照教程做并且分步的去了解所涉及的知识点是很重要的,下面就详细的介绍整个过程。
在创建之前先看一下工程的目录结构:
主要注意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<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")); String ss = info.toString(); infolist.add(ss); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ new CloseDbConn().close(ct,rs,ps); } return infolist; } }
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 block e.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 exception e.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<String> getInfoList(double x,double y){ return InfoDao.getInfoList(x,y); } }
在项目中点击右键,点击other,找到web service client 点击,出现下图
可以选择通过目录中的例如H:\Workspaces\MyEclipse 10\InfoGpsService\WebRoot\WEB-INF\wsdl\InfoServiceService.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)); } }可以进行简单的测试。
未完待续....
Android个人学习笔记-使用myeclipse快速创建webservice并在Android中调用
标签:
原文地址:http://blog.csdn.net/iceyung/article/details/45647411