Apache HttpClient 4.0 在 Android 上无法超时套接字

发布于 2024-11-30 15:19:34 字数 1155 浏览 0 评论 0原文

我正在开发一个 Android 应用程序,该应用程序需要使用 HttpClient 才能将文件从 Android 设备上传到 Web 服务器。我上传的文件大小最大可达 1 Gb,因此如果设备在上传过程中失去连接,可能会发生超时。奇怪的是,我为套接字设置的超时似乎没有任何效果。每当我失去连接时,应用程序就会挂起,而不是引发 SocketTimeoutException。

我尝试使用:

HttpConnectionParams.setConnectionTimeout(params, CrashLogParams.TIMEOUT);
HttpConnectionParams.setSoTimeout(params, CrashLogParams.TIMEOUT);

但这仅适用于连接超时,不适用于套接字超时。我还尝试过:

HttpParams p = httpclient.getParams();
p.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
p.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);

我知道连接超时有效的原因是,执行后我会收到连接超时的异常。

httpclient.execute(httppost);

当上传期间连接丢失时,但在应用程序成功连接到服务器之后,应用程序似乎挂起。

为了测试我的应用程序,我在不同时间禁用了网络,以查看应用程序的反应。如果我在发送请求之前禁用网络,则会收到连接错误,并且我的应用程序可以正常处理该错误,但如果我在上传过程中禁用网络,应用程序将挂起。当然,我是通过 AsyncTasks 执行所有这些请求,因此主 UI 线程不会崩溃。我只是想知道是否有其他方法可以确保套接字在未接收到任何数据时超时,或者我是否在这里遗漏了任何内容。我读过很多博客和帖子,但大多数只是建议使用 SO_TIMEOUT ,这对我不起作用。

sotimeout 在多部分 http 帖子中不起作用在 Android 2.1 上

I'm working on an Android application that requires the use of HttpClient in order to upload a file from the Android device to a web server. The file I'm uploading can reach sizes up to 1 Gb, and so timeouts can occur if the device loses connection during the upload. The weird thing is that the timeout I set for the socket doesn't seem to have any effect. The application would just hang whenever I lose connection instead of raising the SocketTimeoutException.

I tried using:

HttpConnectionParams.setConnectionTimeout(params, CrashLogParams.TIMEOUT);
HttpConnectionParams.setSoTimeout(params, CrashLogParams.TIMEOUT);

but this only worked for the connection timeout, not the socket timeout. Also I tried:

HttpParams p = httpclient.getParams();
p.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
p.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);

The reason I know that connection timeout works is because, I would get the exception for connection timeout after executing

httpclient.execute(httppost);

The application seems to hang when connection is lost during upload, but after the application has successfully made a connection to the server.

To test my app, I disabled the network at different times to see how the application would react. If I disable the network before sending the request, I get a connection error and my application can gracefully handle that, but if I disable it during upload the application hangs. Of course I'm doing all these requests through AsyncTasks, so the main UI Thread doesn't crash. I'm just wondering if there is any other way to make sure that the socket will timeout upon not receiving any data, or if I'm missing anything here. I've read many blogs and posts, but most of them just suggest using SO_TIMEOUT which doesn't work for me.

sotimeout not working in a multipart http post on android 2.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-12-07 15:19:34

您正在创建自己的 ClientConnectionManager 吗?看一下 AndroidHttpClient。 newInstance()

他们创建一个新的 BasicHttpParams 对象并将其传递给 ThreadSafeClientConnectionManager 和 DefaultHttpClient 的构造函数。除了构造函数之外,我看不到在 ClientConnectionManager 上设置参数的方法。

Are you creating your own ClientConnectionManager? Take a look at the source for AndroidHttpClient.newInstance().

They create a new BasicHttpParams object and pass it to the constructors for both a ThreadSafeClientConnectionManager and the DefaultHttpClient. I don't see a way to set parameters on the ClientConnectionManager except in the constructor.

梦亿 2024-12-07 15:19:34

我和你面临同样的问题,具有相同的用例。它发生在运行 android 2.3.6 的三星 Galaxy s2 上,但在 4.x 上的同一设备上则不然。不幸的是,这正是我的客户使用的设备,并且它在大约 10 个具有各种 Android 版本和构造函数的其他测试设备上运行良好...

我花了几个小时尝试使用 HttpUrlConnection 库而不是 Apache 的 HttpClient,但是最终结果是一样的。 AndroidHttpClient 显示相同的行为。这让我说这听起来像是硬件实现或操作系统相关的问题...

我发现的唯一解决方法是将 HttpClient.execute() 方法放在单独的线程中并调用 thread.join(timeout) 作为一种安全措施,可以在出现问题时停止线程。缺点是当上传运行正常但时间超过超时时,请求会被中断...

如果您同时发现了一些东西,如果您能分享它,我将不胜感激。

I'm facing the same problem as you, with the same use case. It happens on a samsung galaxy s2 running android 2.3.6 but not with the same device on 4.x . Unfortunately this is exactly the device my customer uses, and it runs fine on roughly 10 other test devices with various Android versions and constructors...

I spent hours trying with HttpUrlConnection library instead of HttpClient from Apache, but the end result is the same. AndroidHttpClient shows the same behavior. This leads me to say that it sounds like an hardware implementation or OS related problem...

The only workaround I found was to put the HttpClient.execute() method in a separate thread and call thread.join(timeout) as a security to stop the thread if anything goes wrong. The drawback is when upload runs fine but takes longer than the timeout, the request is interrupted...

If you found something in the meantime, I would greatly appreciate if you could share it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文