这里是我的空间,是我用来记录点滴的沃土...

Ksoap2-android 添加网络超时的实现

  最近做一个项目,用的Ksoap2进行的WebService调用,开始的时候没有发现什么问题,后来测试的时候发现,当在测试环境没有外网ip的时候进行登录的时候总是提示正在登录,很长时间没有反应。因为当时手机用的是3G网络,根本访问不到测试的地址,这时问题就这样出现了,查看Ksoap的API根本没有设置网络超时的相关方法,只能求助网络进行解答,可是费劲了各种力量还是没有找到可行的解决方案,最后在仔细观察Ksoap源代码的时候却发现了其中的端倪,最后的解决方法也就这样有了,下面给出自己的解决方案,如果您有幸碰到了这个问题而且正在不知道怎么解决发愁的时候,希望这篇文章能给您一些帮助,本来这篇文章很早就该写的,可是最近太忙了,没来得及写,下面是我的实现方式(我是通过继承类进行了重写):

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

package com.miteno.fpsearch.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.ksoap2.transport.ServiceConnection;

/**
 * 继承了HttpTransportSE ,添加请求超时的操作
 * 
 * @author wsx
 * 
 */

public class ServiceConnectionSE implements ServiceConnection {
    private HttpURLConnection connection;

    public ServiceConnectionSE(String url) throws IOException {
        this.connection = ((HttpURLConnection) new URL(url).openConnection());
        this.connection.setUseCaches(false);
        this.connection.setDoOutput(true);
        this.connection.setDoInput(true);
    }

    public void connect() throws IOException {
        this.connection.connect();
    }

    public void disconnect() {
        this.connection.disconnect();
    }

    public void setRequestProperty(String string, String soapAction) {
        this.connection.setRequestProperty(string, soapAction);
    }

    public void setRequestMethod(String requestMethod) throws IOException {
        this.connection.setRequestMethod(requestMethod);
    }

    public OutputStream openOutputStream() throws IOException {
        return this.connection.getOutputStream();
    }

    public InputStream openInputStream() throws IOException {
        return this.connection.getInputStream();
    }

    public InputStream getErrorStream() {
        return this.connection.getErrorStream();
    }

    // 设置连接服务器的超时时间,毫秒级,此为自己添加的方法
    public void setConnectionTimeOut(int timeout) {
        this.connection.setConnectTimeout(timeout);
    }
}

 

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

package com.miteno.fpsearch.utils;

import java.io.IOException;

import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.ServiceConnection;

/**
 * 继承了HttpTransportSE ,添加请求超时的操作
 * 
 * @author wsx
 * 
 */
public class MyAndroidHttpTransport extends HttpTransportSE {

    private int timeout = 30000; // 默认超时时间为30s

    public MyAndroidHttpTransport(String url) {
        super(url);
    }

    public MyAndroidHttpTransport(String url, int timeout) {
        super(url);
        this.timeout = timeout;
    }

    @Override
    protected ServiceConnection getServiceConnection() throws IOException {
        ServiceConnectionSE serviceConnection = new ServiceConnectionSE(url);
        serviceConnection.setConnectionTimeOut(timeout);

        return serviceConnection;
    }
}

然后接下来是使用:

    //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);//第二个参数就是超时时间

 

 

posted @ 2013-10-28 20:11  wsx2miao  Views(8345)  Comments(5Edit  收藏  举报
这里是我的空间,是我用来记录点滴的沃土...