AndroidHttpTransport 设置连接超时

AndroidHttpTransport 这个方法没有直接设置连接超时  最后我搜了一下  可以换一个jar 来解决这个问题 。

下载地址: http://download.csdn.net/detail/cnryc/7695437#comment


然后调用

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



引用其他:

最近做一个项目,用的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);
                

这样问题就解决了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Redis中,设置连接超时时间是为了主动回收空闲连接,以避免空闲连接占满连接池导致服务崩溃。默认情况下,Redis不会主动断开连接,即使客户端已经空闲了很长时间。为了配置连接超时时间,可以使用timeout参数。timeout参数的单位是秒(s),取值范围为0~100000,其中0表示无限制。建议在业务核心应用中设置合适的timeout值来具备主动回收资源的能力,避免整个业务的混乱和严重后果。 需要注意的是,在实际运行中,Redis不一定会精确地按照timeout的值规定的时间来断开空闲连接。例如,设置timeout为10s,但空闲连接可能在12s后,服务器中新增很多连接时才会被断开。为了降低这个延迟,可以适当增大hz参数的值,提高负责断开超时连接的Redis定时任务的运行频率。 如果不设置超时时间,连接可能会长时间占用,可能导致报错:"could not get a resource from the pool"。因此,在使用Redis时,建议配置适当的连接超时时间,以确保连接资源的有效管理和服务的正常运行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [设置Redis客户端连接超时时间](https://blog.csdn.net/zzchances/article/details/116223555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值