有關java調用Service接口

webservice的調用,常用的大約有3種方式:
    1、使用axis調用
    2、使用xfire調用
    3、使用cxf調用
本文先介紹axis調用

先上代碼:

package com.java;
import java.net.MalformedURLException;  
import java.rmi.RemoteException;  
import org.apache.axis.client.Service;  
import org.apache.axis.client.Call;  
import javax.xml.namespace.QName;  
import javax.xml.rpc.ParameterMode;  
import javax.xml.rpc.ServiceException;  

public class WebServiceTest {
	public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException  {

			  //標識Web Service的具體路徑
			  String endpoint="http://localhost/EMRToMobileService/EMRToMobileService.asmx?WSDL";
			  //發佈的wsdl裏的targetNamespace裏的url
			  String nameSpaceUri = "http://tempuri.org/";
		      Call call;   
		      Object res = "";  
		      // 創建 Service實例
		      Service service = new Service();  
		       try { 
		    	// 通過Service實例創建Call的實例
		         call = (Call)service.createCall();  
		         String strPatientID="415555";
		         String strEventNo="1";
		       //將Web Service的服務路徑加入到call實例之中.
		          call.setTargetEndpointAddress(new java.net.URL(endpoint)); 
		          //發佈的方法名 
		          call.setOperationName(new QName(nameSpaceUri,"GetEMRList"));   
		          //設置參數,主要要和net接口中的名稱一致,類型儘量用String,注意QName中參數的設置  
		           call.addParameter(new QName("http://tempuri.org/","strPatientID"), org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);  
		           call.addParameter(new QName("http://tempuri.org/","strEventNo"), org.apache.axis.Constants.XSD_STRING,ParameterMode.IN);  
		           call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);  
		           call.setUseSOAPAction(true);  
		         //這裏注意有方法名,已經引用的地址。從net接口中找 ,注意地址的寫法 
		           call.setSOAPActionURI("http://tempuri.org/GetEMRList");  
		           res =call.invoke(new Object[]{strPatientID,strEventNo});  
		          //返回值String  
		          System.err.println("method:"+res);  
		     } catch (ServiceException e) {  
		          e.printStackTrace();  
		      } catch (MalformedURLException e) {  
		          e.printStackTrace();  
		       } catch (RemoteException e) {  
		         e.printStackTrace();  
		       }catch (Exception e) {  
		         e.printStackTrace();  
		      }  
		     finally {  
		      }  
		}

}

然後將XML文件解析出來,獲取需要的參數值。

這裏的寫法模式基本上是固定的,最需要注意的就是傳入的地址和參數的寫法,否則很容易就報錯了。

兩種報錯:

(1){http://xml.apache.org/axis/}HttpErrorCode:404
(404)Not Found

不是你的webservice沒有發佈成功,就是你的url輸入錯了,仔細去檢查下

(2) faultDetail:
    {http://xml.apache.org/axis/}stackTrace:服務器無法處理請求。 ---> ORA-00936: 缺失表達式

這種情況則是參數沒有設置正確,導致傳入參數是報錯

推薦博客:http://www.tuicool.com/articles/6NjqIn

                   http://blog.csdn.net/quwei7515/article/details/17952085

                   http://zhidao.baidu.com/link?url=35bzoFNmQCM-AGt_m5F8-oC1EW3q-X2HborpRZ1zbwW36bpPbUGnblGQDUeIYA_5xwJE4LlEfWOkjG7VoyTq_Xlvi-lq8VMKjje8jR52K6W


發佈了25 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章