当前位置: 首页 > 工具软件 > wsdl2ksoap > 使用案例 >

webservice接口开发中Ksoap2和HttpTransportSE的一点使用

萧和同
2023-12-01

首先是实现ServiceConnection接口,实现其中的一系列方法,代码如下:

[java]  view plain  copy
  1. package com.miteno.fpsearch.utils;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.OutputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8.   
  9. import org.ksoap2.transport.ServiceConnection;  
  10.   
  11. /** 
  12.  * 继承了HttpTransportSE ,添加请求超时的操作 
  13.  *  
  14.  * @author wsx 
  15.  *  
  16.  */  
  17.   
  18. public class ServiceConnectionSE implements ServiceConnection {  
  19.     private HttpURLConnection connection;  
  20.   
  21.     public ServiceConnectionSE(String url) throws IOException {  
  22.         this.connection = ((HttpURLConnection) new URL(url).openConnection());  
  23.         this.connection.setUseCaches(false);  
  24.         this.connection.setDoOutput(true);  
  25.         this.connection.setDoInput(true);  
  26.     }  
  27.   
  28.     public void connect() throws IOException {  
  29.         this.connection.connect();  
  30.     }  
  31.   
  32.     public void disconnect() {  
  33.         this.connection.disconnect();  
  34.     }  
  35.   
  36.     public void setRequestProperty(String string, String soapAction) {  
  37.         this.connection.setRequestProperty(string, soapAction);  
  38.     }  
  39.   
  40.     public void setRequestMethod(String requestMethod) throws IOException {  
  41.         this.connection.setRequestMethod(requestMethod);  
  42.     }  
  43.   
  44.     public OutputStream openOutputStream() throws IOException {  
  45.         return this.connection.getOutputStream();  
  46.     }  
  47.   
  48.     public InputStream openInputStream() throws IOException {  
  49.         return this.connection.getInputStream();  
  50.     }  
  51.   
  52.     public InputStream getErrorStream() {  
  53.         return this.connection.getErrorStream();  
  54.     }  
  55.   
  56.     // 设置连接服务器的超时时间,毫秒级,此为自己添加的方法  
  57.     public void setConnectionTimeOut(int timeout) {  
  58.         this.connection.setConnectTimeout(timeout);  
  59.     }  
  60. }  


 

接下来是要继承HttpTransportSE 类,覆写其中的getServiceConnection方法,代码如下:

[java]  view plain  copy
  1. package com.miteno.fpsearch.utils;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.ksoap2.transport.HttpTransportSE;  
  6. import org.ksoap2.transport.ServiceConnection;  
  7.   
  8. /** 
  9.  * 继承了HttpTransportSE ,添加请求超时的操作 
  10.  *  
  11.  * @author wsx 
  12.  *  
  13.  */  
  14. public class MyAndroidHttpTransport extends HttpTransportSE {  
  15.   
  16.     private int timeout = 30000// 默认超时时间为30s  
  17.   
  18.     public MyAndroidHttpTransport(String url) {  
  19.         super(url);  
  20.     }  
  21.   
  22.     public MyAndroidHttpTransport(String url, int timeout) {  
  23.         super(url);  
  24.         this.timeout = timeout;  
  25.     }  
  26.   
  27.     @Override  
  28.     protected ServiceConnection getServiceConnection() throws IOException {  
  29.         ServiceConnectionSE serviceConnection = new ServiceConnectionSE(url);  
  30.         serviceConnection.setConnectionTimeOut(timeout);  
  31.   
  32.         return serviceConnection;  
  33.     }  
  34. }  


 

然后接下来是使用:

[java]  view plain  copy
  1. //AndroidHttpTransport httpTranstation = new AndroidHttpTransport(serviceUrl + "?WSDL"); 这里注释掉,原始的调用方法 MyAndroidHttpTransport httpTranstation = new MyAndroidHttpTransport(serviceUrl + "?WSDL", 1000 * 80);  


 

补充内容:

今天发现ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar把这个问题修复了可以直接来设置超时时间了

HttpTransportSE se=new HttpTransportSE(url, timeout);//第二个参数就是超时时间
 类似资料: