分类: Java
2009-06-09 15:33:33
下载 ,解压,拷贝lib下的jar包
需下载activation.jar和mail.jar,否则在生成Java文件和调用的时候会出现异常:- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
下载javamail和jaf,解压出mail.jar和activation.jar
2. wsdl2Java ant脚本
WSDL文件的下载地址参见:
打开并保存为GoogleSearch.wsdl到工程axis-1_4工程目录下
--------------------------------------------------------------------
set JAVA_HOME = C:\j2sdk1.4.2_09
set PATH = %JAVA_HOME%\bin;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib;
set CLASSPATH = %JAVA_HOME%\lib;%JAVA_HOME%\jre\lib;
set Axis_Lib=E:\ming\workspace\axis-1_4\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:\ming\workspace\axis-1_4\src
set Package=com.google.api
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% GoogleSearch.wsdl
--------------------------------------------------------------------
解析:
set Axis_Lib=E:\ming\workspace\axis-1_4\lib //Axis jar包存放的文件夹
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:\ming\workspace\axis-1_4\src //eclipse中axis-1.4工程路径
set Package=com.google.api //生成的java文件存放的包
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% GoogleSearch.wsdl //开始将GoogleSearch.wsdl生成为java文件
3. 编写客户端调试
package com.google.api;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class ClientGoogle {
public static void main(String[] args) throws ServiceException,
RemoteException {
String clientKey = "---------------key-----------------";
GoogleSearchServiceLocator service = new GoogleSearchServiceLocator();
GoogleSearchPort_PortType gsp = service.getGoogleSearchPort();
//Google Search API
GoogleSearchResult searchResult = gsp.doGoogleSearch(clientKey,
"", 0, 10, false, "", false, "", "latin1",
"latin1");
System.out.println("Google Search Results:");
System.out.println("======================");
ResultElement results[] = searchResult.getResultElements();
for (int counter = 0; counter < results.length; ++counter) {
ResultElement result = results[counter];
String returnResult = "('" + result.getTitle() + "','"
+ result.getURL() + "','" + result.getSnippet() + "')";
System.out.println("Return Result");
System.out.println(returnResult);
}
//Google CachedPage
byte[] cachedBytes = gsp.doGetCachedPage(clientKey,
"");
System.out.println("Cached page:");
System.out.println("============");
String cachedString = new String(cachedBytes);
System.out.println(cachedString);
//Google SpellingSuggestion
String suggestion = (String) gsp.doSpellingSuggestion(clientKey,
"williamlong");
System.out.println(suggestion);
}
}