几种常用接口调用方式

个人总结下几种常用的接口调用方式,具体看对方提供的是什么样的webService接口,如有错误,欢迎指正;

1、以前玩微信公众帐号开发的时候,调用过百度翻译的接口,就是这种形式的接口:

/**
* 翻译(中->英 英->中 日->中 )

* @param source
* @return
*/
public static String translate(String source) {
String dst = null;


// 组装查询地址
String requestUrl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=CaHtZCTje6XaaYp1tSZN4RAa&q={keyWord}&from=auto&to=auto";
// 对参数q的值进行urlEncode utf-8编码
requestUrl = requestUrl.replace("{keyWord}", urlEncodeUTF8(source));


// 查询并解析结果
try {
// 查询并获取返回结果
String json = httpRequest(requestUrl);
// 通过Gson工具将json转换成TranslateResult对象
TranslateResult translateResult = new Gson().fromJson(json,
TranslateResult.class);
// 取出translateResult中的译文
dst = translateResult.getTrans_result().get(0).getDst();
} catch (Exception e) {
e.printStackTrace();
}


if (null == dst)
dst = "翻译系统异常,请稍候尝试!";
return dst;
}

/**
* 发起http请求获取返回结果

* @param requestUrl
*            请求地址
* @return
*/
public static String httpRequest(String requestUrl) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url
.openConnection();


httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);


httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();


// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);


String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();


} catch (Exception e) {
}
return buffer.toString();
}


2、CXF接口调用;


public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ICXFService.class);
factory.setAddress("http://localhost:8081/ws/cxf/services/helloworld?wsdl");
ICXFService client = (ICXFService) factory.create();
System.out.println(client.sayHello("andrewLau"));   //sayHello为接口提供方法;
}


3、AXIS接口调用


public static void main(String[] args) {
try {
String url = "http://localhost:8081/axis/services/HelloHandler?wsdl";
Service serv = new Service();
Call call = (Call) serv.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(url, "hello"));
String result = (String) call.invoke(new Object[] { "xiexx" });  //invoke为接口提供方法
System.out.println(result);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}


4、XFIRE接口调用R

public static void main(String[] args) throws Exception {
String url = "http://localhost:8081/ws/services/HelloRmi";
Service serv = new ObjectServiceFactory().create(IHelloRmi.class);
IHelloRmi service = (IHelloRmi) new XFireProxyFactory().create(serv,
url);
String result = service.hello("xiexx");  //hello为接口提供方法
System.out.println(result);
}


5、还有一种方式是利用eclipse的webservice client,直接用wsdl地址自动生成,不需要管到底是什么接口的,直接调用

你可能感兴趣的:(几种常用接口调用方式)