Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android個人學習筆記-使用myeclipse快速創建webservice並在Android中調用

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

編輯:關於Android編程

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

很早之前就使用過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();
	     	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;
	}
}

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 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 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));
	}
}
可以進行簡單的測試。

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved