2012年4月17日 星期二

轉貼 WSDL CLient in JAVA

WSDL 是一種網路服務傳送接收的方法,以XML 樣視為基礎的溝通方式[1],

關於建立的方式這裡就不介紹了,以client 端為主, 詳細流程不多說,

搜尋 wsdl eclipse clinet 就能找到相關的資料了,值得提醒的地方就是大部分都是用 AXIS2 來搭配解析[2], 在 eclipse 中要使用的話要先下載後在設定路徑 , Windows->Preference->Web Services,  AXIS2 載點(http://axis.apache.org/axis2/java/core/download.cgi)。




















圖一,設定AXIS

此外還有,
a. 在有限制的環境下,要套用proxy.
b. 也可以使用 AXIS 的方式取得 WSDL, AXIS 至少在 eclipse3.6 是內建的


以下簡介使用的步驟:

  • 新增web專案TEST_WSDL_Client,我是使用 Dynamic Web Project, static project 應該也可以吧。
  • 在TEST_WSDL_Client專案下新增 Web Service Client 如圖二。
  • 設定 Web Service Client,Service definition 輸入 WSDL 的網址, Configuration 設定AXIS2,預設是AXIS,設定畫面如圖三。
  • 若是在有proxy 的環境下記得設定proxy,接者按下一步應該就可以產生相關的java class 了
















圖二,新增WSDL Client














圖三,選擇輸入WSDL 服務的網址,設定APACHE 和 AXIS2 的server





補充 , 若要將產生的class 檔 在 client 端程式使用,必須使用 AXIS2 的lib ,不過查詢一下找到精簡的jar 清單[3], 清單如下:


  • axiom-api-1.2.12.jar
  • axiom-impl-1.2.12.jar
  • axis2-adb-1.6.1.jar
  • axis2-kernel-1.6.1.jar
  • axis2-transport-http-1.6.1.jar
  • axis2-transport-local-1.6.1.jar
  • axis2-xmlbeans-1.6.1.jar (optional)
  • commons-codec-1.3.jar
  • commons-httpclient-3.1.jar
  • commons-logging-1.1.1.jar
  • httpcore-4.0.jar
  • mail-1.4.jar
  • neethi-3.0.1.jar
  • woden-api-1.0M9.jar
  • woden-impl-dom-1.0M9.jar
  • wsdl4j-1.6.2.jar
  • wstx-asl-3.2.9.jar
  • xmlbeans-2.3.0.jar (optional)
  • XmlSchema-1.4.7.jar




參考:
[1] WSDL,wiki
[2] AXIS2
[3] Minimum set of jar files for Apache Axis2 1.6.1

2012年4月3日 星期二

心得 Struct2

最近又報告一次,整理一下投影片放在這裡

2012年3月28日 星期三

心得, java 取得音樂撥放時間長度


取得 音效檔的長度,

Wav: 

可以是用[1] 的方式,計算出音效檔的長度,程式碼:


private  Void GetLength(String filename) { // ex "C:\\Documents and Settings\\Administrator\\My Documents\\My Music\\my.wav"

  File file = new File(filename);   

  AudioInputStream audioInputStream;
  try {
audioInputStream = AudioSystem.getAudioInputStream(file);
AudioFormat format = audioInputStream.getFormat();
long audioFileLength = file.length();
int frameSize = format.getFrameSize();
float frameRate = format.getFrameRate();
float durationInSeconds = (audioFileLength / (frameSize * frameRate));
System.out.println( " Length :"+durationInSeconds);

} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 



MP3: 

利用 MP3SPI[2],這個LGPL 的  函式取得 mp3 檔案的標籤,其中就有音效檔的長度





參考:
[1]: How do I get a mp3 file's total time in Java?, http://stackoverflow.com
[2]: MP3SPI 


2012年3月21日 星期三

轉貼 AJAX cache更新問題

自己要用到一個AJAX 更新資料的網頁發現的,預設的cache 是打開得,所以會存放上次的資料,但是我是要每一段就要更新的,所以必需要找個方式吧cache 關掉或繞過他才能達到我想要得效果。

在*中有提到四個方式:


1.在請求的URL後面加一個時間參數,如: time=new date() 當然也可以添加其他性質參數,只要是隨機參數就可以,
open("GET",url+"?t="+Math.random(),false) 或者 url+"?timeStamp="+new Date().getTime();  

2.  新增 js 代碼   
  function ajaxRead(file){ var xmlObj = null; if(window.XMLHttpRequest){ xmlObj = new XMLHttpRequest(); } else if(window.ActiveXObject){ xmlObj = new ActiveXObject("Microsoft.XMLHTTP"); } else { return; }  xmlObj.onreadystatechange = function(){ if(xmlObj.readyState == 4){ processXML(xmlObj.responseXML); } else{ document.getElementById ('playernews').innerHTML='採用AJAX來實現資料的讀取,正在載入...'; } } xmlObj.open ('GET', file, true); xmlObj.send (''); } function show() { ajaxRead('*.jsp'); setInterval("ajaxRead('new.php')",30000); //自動更新 }   

3.加上
xmlhttp.setRequestHeader("Cache-Control","no-cache");  

4.在XmlHttpRequest發送請求之前加上 XmlHttpRequest.setRequestHeader("If-Modified-Since","0"), 如:在 XXXXX.send(YYYYYY).



我沒有全部利用過,只使用過1就能達成效果。

參考:* 解決Ajax更新不及時的途徑,IT專家網

2012年3月20日 星期二

心得, 網站播放音效

要在網頁上播放音樂有很多方式,在[1]中介紹了許多方式如:




  • 使用API 如 yahoo API(sample) , Google (sample)
  • Using Plugins, 不同瀏覽器有不同的plugin,這裡我知道的有quick time, flash
  • 使用 embed 標籤,像ie 的話是直接call mediaplay 播放的樣子,Firefox 需要額外裝Quick time的外掛
  • 使用 object 標籤, 沒用過
  • 使用 audio 標籤, HTML5方式[2], 好用,但是IE6~8 要使用的話需要額外設定,這裡就不深入討論
  • 其他



其他相關找到的用法:

  •  [3] 也是用 HTML5
  • [4] 利用flash 來播放

為了單純方便播放聲音,我簡單利用jquery 和embed 的方式寫一個 呼叫聲音的函式,測試過 Firefox 需要額外裝Quick time的外掛, IE 和 Chrome 可以直接播放。

function JPlaySound(_soundlocation){
// _soundlocation like "music/sample.mp3"
$('embed#objJSound').remove(); // remove duplicate tag
var $newdiv = $('<embed autostart="true" hidden="true" id="objJSound" src="'+_soundlocation+'"></embed> '); // hidden embed tag
$('body').append($newdiv); // append to body
}





----------------------------------------------------------------------------------------
補充1  純粹使用 javascript 的 code


function JPlaySound(_soundlocation){
 // delete old tag if exist 
var olddiv = document.getElementById('objJSound');
if(olddiv!=null)
 document.body.removeChild(olddiv);
 var newdiv = document.createElement('embed');
 newdiv.setAttribute("id","objJSound");
 newdiv.setAttribute("src",_soundlocation);
 newdiv.setAttribute("hidden","true");
 newdiv.setAttribute("autostart",true);
 document.body.appendChild(newdiv);
 }


---------------------------------------------------------------------------------------
補充2 混用 HTML5 (支援ie 6,7,8)



設定 tag 和判斷是否支援HTML5 // HTML5 audio tag
<!-- html5 tag -->
 <audio id="a1" style="visibility:hidden;" controls> </audio>


 // 判斷是否支援HTML5


 <script type="text/javascript">
 var isSupportHTML5 = true;
 </script>









 // 不支援 (小於ie9)將 isSupportHTML5 設為 false



 <!--[if lt IE 9]>
 <script type="text/javascript">
 //您正在使用的瀏覽器為 ie9 或比 ie9 更新的版本 
 isSupportHTML5 = false;
 </script>
 <!--<![endif]-->



呼叫程式:
/** _soundlocation like "music/sample.mp3" 
 // Firefox 不支援 mp3 **/ 
 function JPlaySound(_soundlocation){
 if(isSupportHTML5){
 var audio = document.getElementById("a1");
 audio.src= _soundlocation;
 audio.load();
 audio.play();
 } else{
 // delete old tag if exist
 var olddiv = document.getElementById('objJSound');
 if(olddiv!=null)
   document.body.removeChild(olddiv);
 var newdiv = document.createElement('embed');
 newdiv.setAttribute("id","objJSound");
 newdiv.setAttribute("src",_soundlocation);
 newdiv.setAttribute("hidden","true");
 newdiv.setAttribute("autostart",true);
 document.body.appendChild(newdiv);
 }
 }


參考:
[1] W3cSchools.com, HTML Audio Sounds
[2] HTML5 audio tag
[3] jPlayer
[4] flashsound 
[5] Conditional Comments [if IE] : IE 專用 (IE only) 條件式 HTML 註解的語法
[6] Audio and Video processing in HTML5