Android personal learning notes-use myeclipse to quickly create a webservice and call it in Android
Web service is a platform-independent, low-coupling, self-contained, programmable web-based application that can use open XML (a subset of standard General Markup Language) standards are used to describe, publish, discover, coordinate, and configure these applications for Distributed interoperability applications. (Source: Baidu encyclopedia)
A long time ago, myeclipse was used to create a webservice (Java API for XML Web Services (JAX-WS) for JAX-WS specifications, a Java programming language API for creating Web Services. Click to open the link). There are also many examples available in Baidu for reference. As a beginner, it is very important to follow the tutorial step by step and learn the knowledge points involved step by step, the entire process is described in detail below.
1. Create a webservice
Before creating a project, let's take a look at the directory structure of the project:
Pay attention to the two options for JAX-WS 2.1, which should be manually added after creation, which will be explained later.
This project is mainly used to communicate with Android. It mainly has an interface, a getInfoList (double x, double y) method, where x and y are the longitude and latitude, and the latitude and longitude obtained through Android are located, then, calculate the information from the database, and obtain the distance between different locations. Then, return a json string from the client. For the format type of the string, see net. zmqc. bean Information class.
This project is mainly connected to the database, so different classes need to be stored in packages, but the external interface uses net. zmqc. and paste all the classes in the service. Before that, we can use a simple test class to demonstrate the process of creating a webservice, this step requires you to write the class you want to use. For example, you can simply write a Test class and define one or more methods in the Test class.
public class Test {public static String getReply(String ss){return ss;}}
Right-click the project and you will see
Click other;
Click web service and the following figure is displayed. Select from class and click Next:
Find the class you want to create, select Generate WSDL in project, and click OK, as shown in:
Then you can see the new content in your project:
Click configure Build Path in Build Path, as shown in figure
Click add library... to find MyEclipse Librarys, and click the content at the bottom of the page.
The release of this simple test class service is complete. Next, let's take a look at the classes in the project for Android in the initial project.
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();}}}
This class calculates the two-point distance by longitude and latitude, mainly for specific needs.
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 the class in this package is also like the Test class, publish it
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);}}
Then, you can quickly establish a client to test whether the service is successful and debug the service.
Right-click the project, click other, find web service client, and click.
You can select H: WorkspacesMyEclipse 10InfoGpsServiceWebRootWEB-INFwsdlInfoServiceService.wsdl from the directory.
Or directly through http: // localhost: 8080/InfoGpsService/InfoServicePort? Wsdl.
Then you can call the test class.
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));}}
You can perform a simple test.