Android Personal Learning Note-use MyEclipse to quickly create webservice and invoke it in Android

Source: Internet
Author: User

Web Service is a platform- independent, low-coupling, self-contained , programmable Web-based application that can use open XML( A subset of standard common markup languages) Standards to describe , publish, Discover, orchestrate, and configure these applications for the development of distributed, interoperable applications . (Source Baidu Encyclopedia)

Used MyEclipse to create a JAX-WS specification WebService (Java API for XML Web Services (JAX-WS) is a Java programming language API for creating WEB services. Click to open the link ), in Baidu also has a lot of ready-made examples for reference, as a beginner, step by step in accordance with the tutorial to do and step to understand the knowledge involved in the point is very important, the following detailed introduction of the whole process.

1, the creation of WebService

Look at the catalog structure of the project before you create it:


Note the two options for Jax-WS 2.1, which should be added manually after the creation is complete, and then explained.

This project is mainly with the Android communication, there is an interface, a getinfolist (double x,double y) method, where x and Y are latitude and longitude, through the Android side positioning to obtain the latitude and longitude, and then with the database side of the information to calculate, That is to get a different location away from me, and then return the client a JSON string, the format type of the string is shown in the Net.zmqc.bean information class.

This project is mainly connected with the database, so it is necessary to store different classes of subcontracting, But the external interface uses the class in the Net.zmqc.service, the following to paste all the classes, and then the first through a simple test class to explain the process of creating a webservice, this step is to write the class you want to use, for example, you can write a test class, and then in the test class to define a Method or Multiple methods

public class Test {public static string getreply (string ss) {return SS;}}
Then right-click on the item and you can see

Click on other to appear;

Click Web Service to see the following diagram, select from class, and then click Next:



Find the class you need to create, Generate WSDL in Project This tick, then click OK to finish, such as:

Then you can see what's new in your project:

Then click on configure build path in the build path and click


Then click Add Library ... Find MyEclipse Librarys, click on the bottom to find the article at the beginning of the content

The release of the service to this simple test class is complete, so take a look at the various classes in the project for Android in the beginning of the 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<string> getinfolist (Double x,double y) {list<string> infolist = new arraylist<string> (); Information info = new information (); try {ct = new dbconn (). g     Etconn (); Stringsql= "SELECT * from information,place where information.g_place = place.p_id";p s=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"));d ouble 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 ();p s=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 ();p s=null;ct.close (); ct=null; System.out.println ("link break!!!");} catch (Exception e) {e.printstacktrace ();}}}
The class is calculated by the latitude of two points, mainly for the specific need to produce
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 "I            Nverse 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-c * 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;                (+) Sinsigma = Math.sqrt (Sinsqsigma); Cossigma = sinu1sinu2 + cosu1cosu2 * COSLAMBDA; Sigma = Math.atan2 (Sinsigma, CosSIGMA); (+) Double Sinalpha = (Sinsigma = = 0)? 0.0:COSU1COSU2 * SINLAMBDA/SINSIGMA;                (+) Cossqalpha = 1.0-sinalpha * SINALPHA; COS2SM = (Cossqalpha = = 0)? 0.0:cossigma-2.0 * SINU1SINU2/COSSQALPHA; () 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 * (7                4.0-47.0 * usquared)); Double C = (f/16.0) * Cossqalpha * (4.0 + f * (4.0-3.0 * cossqalpha));                (ten) 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 * Sin                Sigma * 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)));                (one) 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 Str ing 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_p      Lace\ "=\" "+ 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;pub LIC 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 the package is the same as the test class, it is published

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);}}

You can then test the success of the service by quickly establishing a client and debugging

Right-click on the project and click Other to find the Web service client click, which appears

You can choose to go through the directory for example H:\Workspaces\MyEclipse 10\infogpsservice\webroot\web-inf\wsdl\infoserviceservice.wsdl

or directly through the http://localhost:8080/InfoGpsService/InfoServicePort?wsdl, to get, and finally click to finish it.

You can then call the class by testing the

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 (); Nfoservicedelegate Eif=ef.getinfoserviceport (); System.out.println (Eif.getinfolist (49.327458,121.349691));}}
A simple test is possible.

Not to be continued ....


Android Personal Learning Note-use MyEclipse to quickly create webservice and invoke it in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.