Phonegap文件传输超时

时间:2013-02-05 13:15:22

标签: android cordova

有没有办法在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 个答案:

答案 0 :(得分:7)

他们似乎决定不将此作为一个选项来实现,因此只留下使用abort()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上有超时选项会好得多。

答案 1 :(得分:1)

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如功能请求注释中所述,这可以通过使用setTimeout()

操作abord()来完成。