webservice:使用urlConnection操作webservice
来源:互联网 发布:115网盘会员淘宝 编辑:程序博客网 时间:2024/05/16 19:27
先来两道小题目
1,
public class ABC{
publicvoid abc(Object aa) {
System.out.println(“object...”);
}
publicvoid abc(String aa) {
System.out.println(“aaaaaaaaaaaaa”);
}
publicstatic void main(String[] args) {
ABCa = new ABC();
a.abc(null);
}
}
问:调用哪个方法?
答:调用的是abc(String aa)方法,Java是较小优先原则,因为String是Object的子类,所以调用的是String。
2,面试题:
public class BCD{
publicstatic void main(String[] args) {
for(;;)
Object o = new Object();
}
}
问:红色部分报错,为什么?
在for循环外面加上大括号,(不是死循环,也会出现这种情况)
public class BCD{
publicstatic void main(String[] args) {
for(;;){
Object o = new Object();
}
}
}
就不报错了,为什么?
3,通过urlConnection的方式获取服务器提供的服务。
public class UrlConnDemo {@Testpublic void getAll() throws Exception{URL url = new URL("http://192.168.1.254:2345/hello");HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setRequestMethod("POST");con.setRequestProperty("Content-Type","text/xml;charset=UTF-8");con.setDoInput(true);//可以从服务器上读取消息con.setDoOutput(true);//可以向服务器与参数//读取xml文件,根据xml文件中的信息来访问服务器提供的服务,//比如,通过a.xml文件可以知道访问那个方法InputStream in = UrlConnDemo.class.getResourceAsStream("a.xml");ByteArrayOutputStream bt= new ByteArrayOutputStream();int len = 0;byte[] by = new byte[1024];while(-1!=(len=in.read(by))){bt.write(by, 0,len);}in.close();OutputStream out = con.getOutputStream();out.write(bt.toByteArray());out.close();//从服务器上读取数据InputStream is = con.getInputStream();//BufferedReader br = new BufferedReader(new InputStreamReader(is));//String line = null;//while(null!=(line=br.readLine())){//System.err.println(line);//}SAXReader sax = new SAXReader();Document dom = sax.read(is);List<Element> users = dom.selectNodes("//return");for(Element el:users){String id = el.elementText("id");String nm = el.elementText("name");System.err.println("id:"+id+","+nm);}is.close();con.disconnect();}}
以下是a.xml文件的内容:
<it:Envelope xmlns:it="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><it:Body><q0:getAll /> //调用getAll()方法</it:Body></it:Envelope>
- webservice:使用urlConnection操作webservice
- 使用URLConnection调用webservice
- 使用URLConnection调用webservice
- 使用urlConnection调用webservice
- 通过UrlConnection调用Webservice服务
- WebService基础教程之三(通过UrlConnection调用WebService服务)
- 通过URLConnection的方式访问WebService
- WebService使用
- WebService使用
- webservice 使用
- WebService 使用
- Webservice使用
- webservice使用
- Webservice使用
- 使用Ksoap2-Andorid调用WebService操作步骤
- WebService:编写webService使用实例
- WebService
- WebService
- gvim常用配置
- 按条件输出行
- linux学习入门4——linux文件系統基本结构(linuxcast.net)(倒转树状结构、命名机制、查看文件类型)
- 子父类在内存中的表现形式
- mempodroid 原理分析【转】
- webservice:使用urlConnection操作webservice
- set基本用法
- 15 Linux lsof Command Examples (Identify Open Files)
- RT5350调试总结
- Linux下使用wget下载jdk
- pygame开发的"雷电"游戏雏形
- http使用apache工具类提交数据
- OCP-1Z0-053-V12.02-291题
- iOS界面-仿网易新闻左侧抽屉式交互