如何切断对宏达电(升级Froyo及以下)手机的HttpURLConnection的? [英] How to disconnect an HttpUrlConnection on HTC (Froyo and below) phones?

查看:180
本文介绍了如何切断对宏达电(升级Froyo及以下)手机的HttpURLConnection的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要断开客户端在某些情况下,长轮询HTTP请求。在HttpURLConnection的的相关部分我做的服务器如下(所有code以下是在线程的run()方法):

I need to disconnect a long polling http request from the client side in some cases. The relevant part of the HttpUrlConnection I make to the server is as follows (all the code below is within a Thread's run() method):

try {
    URL url = new URL(requestURL);

    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Accept-Charset", "UTF-8");
    connection.setConnectTimeout(5 * 1000);
    connection.setReadTimeout(60 * 1000);
    connection.setRequestMethod("GET");

    // read the output from the server
    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder stringBuilder = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null) {
        stringBuilder.append(line + "\n");
    }
    Log.d(TAG, stringBuilder);
} catch (IOException ioe) {
    Log.e(TAG, ioe);
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

这是我第一次开始,然后(第二次延迟后)尝试取消请求:

This is how I first initiate, then (after a second delay) try to cancel the request:

pollThread = new PollThread();
pollThread.start();
Log.d(TAG, "pollThread started");

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        pollThread.cancelRequest();
        Log.d(TAG, "pollThread presumably cancelled");
    }
}, 1000);

这是什么cancelRequest()方法如下:

And this is what the cancelRequest() method looks like:

public void cancelRequest() {
    if (connection != null) {
        connection.disconnect();
    }
}

所以基本上,

So essentially,

  1. 在我发起的HttpURLConnection用GET请求,以1分读取超时
  2. 然后在一秒钟之后,我尝试取消该要求
  3. 在预期成果是连接应抛出IOException当我打电话connection.disconnect()

而这正是在各种仿真器(2.2 - 4.0.3)会发生什么,一个摩托罗拉Atrix(2.3.7)和三星注(4.0.1)。但在运行2.2的一些HTC设备,请求将保持活着,这将收到的响应,尽管我明确终止连接。我证实了这一带的HTC Desire和HTC野火。

And this is exactly what happens on various emulators (2.2 - 4.0.3), a Motorola Atrix (2.3.7) and a Samsung Note (4.0.1). But on some HTC devices running 2.2, the request will stay alive and it will recieve the response, despite the fact that I explicitly terminated the connection. I verified this with an HTC Desire and an HTC Wildfire.

这是怎么回事呢?我怎样才能安全地取消这样的要求上运行的所有设备2​​.2 +?

What's going on here? How can I cancel such a request safely on all devices running 2.2+?

为了您的方便,整个code是可在这里,你应该喜欢做试驾自己: https://开头要点.github.com / 3306225

For your convenience, the whole code is available here, should you like to do a test drive yourself: https://gist.github.com/3306225

推荐答案

这是在早期的Andr​​oid版本(升级Froyo 2.2),在排序,插座不能异步地被其他线程关闭一个已知的错误,并已被固定在姜饼2.3:

What's going on here?

This is a known bug in earlier android release (Froyo 2.2) which, in sort, sockets can not be closed asynchronously by other threads, and has been fixed in Gingerbread 2.3:

<一个href="http://$c$c.google.com/p/android/issues/detail?id=11705&can=1&q=HttpUrlConnection%20disconnect&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars"相对=nofollow>问题11705:无法使用时关闭HTTP连接的HttpURLConnection

在该链接的项目成员评论:

Comments from project member in that link:

这方面最好的逼近,将工作在当前版本是设置读取和连接超时的HTTP连接。

The best approximation of this that will work in current releases is to set read and connect timeouts on the HTTP connection.

希望有所帮助。

这篇关于如何切断对宏达电(升级Froyo及以下)手机的HttpURLConnection的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆