2

有没有办法在 phonegap 中定义下载文件传输的超时时间。在 android 设备上,如果我尝试下载无响应的 URL,我会在 8 分钟后得到以下信息:

02-05 11:50:38.234 W/FileTransfer(12658): Error getting HTTP status code from connection.
02-05 11:50:38.234 W/FileTransfer(12658): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.io.InputStream.read(InputStream.java:163)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.Streams.readAsciiLine(Streams.java:201)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-05 11:50:38.234 W/FileTransfer(12658):   at org.apache.cordova.FileTransfer.download(FileTransfer.java:495)
02-05 11:50:38.234 W/FileTransfer(12658):   at org.apache.cordova.FileTransfer.execute(FileTransfer.java:88)
02-05 11:50:38.234 W/FileTransfer(12658):   at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:231)
02-05 11:50:38.234 W/FileTransfer(12658):   at java.lang.Thread.run(Thread.java:856)
02-05 11:50:38.234 W/FileTransfer(12658): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.Posix.recvfromBytes(Native Method)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.Posix.recvfrom(Posix.java:131)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
02-05 11:50:38.234 W/FileTransfer(12658):   at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
02-05 11:50:38.234 W/FileTransfer(12658):   ... 15 more
02-05 11:50:38.234 E/FileTransfer(12658): {"target":"\/storage\/sdcard0\/Android\/data\/uk.ac.edina.mobile\/cache\/edina\/cache\/open_3_21_37.jpg","source":"http:\/\/fieldtripgb.edina.ac.uk\/mapcache\/tms1.0.0\/fieldtripgb@BNG\/3\/21\/37.jpg?version=1.1.1&id=22362b279a637a91&app=free&cache=true","code":3}
02-05 11:50:38.234 E/FileTransfer(12658): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:552)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.io.InputStream.read(InputStream.java:163)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.Streams.readAsciiLine(Streams.java:201)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-05 11:50:38.234 E/FileTransfer(12658):   at org.apache.cordova.FileTransfer.download(FileTransfer.java:495)
02-05 11:50:38.234 E/FileTransfer(12658):   at org.apache.cordova.FileTransfer.execute(FileTransfer.java:88)
02-05 11:50:38.234 E/FileTransfer(12658):   at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:231)
02-05 11:50:38.234 E/FileTransfer(12658):   at java.lang.Thread.run(Thread.java:856)
02-05 11:50:38.234 E/FileTransfer(12658): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.Posix.recvfromBytes(Native Method)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.Posix.recvfrom(Posix.java:131)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
02-05 11:50:38.234 E/FileTransfer(12658):   at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
02-05 11:50:38.234 E/FileTransfer(12658):   ... 15 more
4

4 回答 4

9

似乎他们决定不将其作为选项来实现,因此将唯一的选择保留为使用abort()and setTimeout()

由于在下载/上传上设置任意时间限制似乎很幼稚,我将重置超时fileTransfer.onProgress()

像这样的东西:

var downloadTimer = setTimeout(function () {
    downloadTimerExceeded(fileTransfer);
}, 30000);

fileTransfer.onprogress = function (progressEvent) {
        if (downloadTimer) {
            clearTimeout(downloadTimer); //cancel the previous timer.
            downloadTimer = null;
        }
        downloadTimer = setTimeout(function () {
            downloadTimerExceeded(fileTransfer);
        }, 30000);
};

function downloadTimerExceeded(fileTransfer) {
    fileTransfer.abort();
}

如果他们在 FileTransfer 本身上有一个超时选项会更好。

于 2016-01-21T00:16:33.293 回答
1

不,但这是一个功能请求https://issues.apache.org/jira/browse/CB-2394

于 2013-02-08T10:42:30.590 回答
0

您是否尝试过使用 chunkedMode 选项?

于 2013-02-06T04:49:59.753 回答
0

正如在功能请求评论中所说,这可以通过使用 setTimeout() 操作 abord() 来完成

于 2013-03-19T14:50:25.477 回答